How Does the this Keyword Work?
About 1 min
How Does the this Keyword Work? 관련
The JavaScript Interview Prep Handbook – Essential Topics to Know + Code Examples
JavaScript is a widely used language in web development and powers interactive features of virtually every website out there. JavaScript makes it possible to create dynamic web pages and is very versatile. JavaScript remains one of the most in-demand programming languages in 2024. Many companies are looking for proficiency in...
The JavaScript Interview Prep Handbook – Essential Topics to Know + Code Examples
JavaScript is a widely used language in web development and powers interactive features of virtually every website out there. JavaScript makes it possible to create dynamic web pages and is very versatile. JavaScript remains one of the most in-demand programming languages in 2024. Many companies are looking for proficiency in...
The this
keyword is the object that you are currently referencing. Its value is set to the context in which you are using it. When referenced globally, this
refers to the window object.
console.log(this) // prints window {}
this
can be used to access properties of an object.
const obj = {
name: 'kunal',
age: 21,
getInfo() {
console.log(`Name: ${this.name}, Age: ${this.age}`);
}
};
obj.getInfo();
Refer to the docs to learn more about the this
keyword.