The this keyword in JavaScript refers to the current execution context. This means that the value of
this will vary depending on how and where it is being used.
Here are some of the most common ways that the this keyword is used in JavaScript:
In object methods: In object methods, this refers to the object that the method is called on. For example, the following code defines a method called
fullName on the Person object:
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.fullName = function() {
return this.firstName + " " + this.lastName;
};
var person = new Person("John", "Doe");
console.log(person.fullName()); // "John Doe"
In event handlers: In event handlers, this refers to the element that the event is attached to. For example, the following code defines an event handler that is triggered when the user clicks on an element:
var element = document.getElementById("myElement");
element.onclick = function() {
console.log(this); // The element that was clicked
};
In standalone functions: In standalone functions, this refers to the global object. For example, the following code defines a function that prints the value of
this:
function printThis() {
console.log(this); // The global object
}
printThis(); // The global object
The this keyword can be a bit confusing at first, but it is an important part of JavaScript. By understanding how it works, you can write more effective JavaScript code.
Here are some additional things to keep in mind about the this keyword:
The value of this can be changed using the call(),
apply(), and bind() methods.
The value of this can be different in strict mode and non-strict mode.
The value of this can be different in different browsers.
If you are unsure about the value of this in a particular context, you can use the
console.log(this) statement to print its value. This can be helpful for debugging your code.
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
thiskeyword in JavaScript refers to the current execution context. This means that the value ofthiswill vary depending on how and where it is being used.Here are some of the most common ways that the
thiskeyword is used in JavaScript:thisrefers to the object that the method is called on. For example, the following code defines a method calledfullNameon thePersonobject:thisrefers to the element that the event is attached to. For example, the following code defines an event handler that is triggered when the user clicks on an element:thisrefers to the global object. For example, the following code defines a function that prints the value ofthis:The
thiskeyword can be a bit confusing at first, but it is an important part of JavaScript. By understanding how it works, you can write more effective JavaScript code.Here are some additional things to keep in mind about the
thiskeyword:thiscan be changed using thecall(),apply(), andbind()methods.thiscan be different in strict mode and non-strict mode.thiscan be different in different browsers.If you are unsure about the value of
thisin a particular context, you can use theconsole.log(this)statement to print its value. This can be helpful for debugging your code.