The this keyword in JavaScript refers to the current context. In other words, it refers to the object that the function is being called on. The value of this can change depending on how the function is called.
For example, if a function is called on an object, this will refer to that object. If a function is called without being called on an object, this will refer to the global object.
The this keyword can be used to access properties and methods on the current object. For example, the following code will log the value of the name property on the current object:
Code snippet
function myFunction() {
console.log(this.name);
}
const obj = {
name: "John Doe"
};
myFunction(); // Will log "John Doe"
The this keyword can also be used to call methods on the current object. For example, the following code will call the greet() method on the current object:
Code snippet
function myFunction() {
this.greet();
}
const obj = {
greet() {
console.log("Hello!");
}
};
myFunction(); // Will log "Hello!"
The this keyword is a powerful tool that can be used to access and manipulate the current object.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The this keyword in JavaScript refers to the current context. In other words, it refers to the object that the function is being called on. The value of this can change depending on how the function is called.
For example, if a function is called on an object, this will refer to that object. If a function is called without being called on an object, this will refer to the global object.
The this keyword can be used to access properties and methods on the current object. For example, the following code will log the value of the name property on the current object:
Code snippet
The this keyword can also be used to call methods on the current object. For example, the following code will call the greet() method on the current object:
Code snippet
The this keyword is a powerful tool that can be used to access and manipulate the current object.