this KeywordThe this keyword in JavaScript refers to the object from which the current function is called. Its value is not fixed; it depends on how and where the function is invoked: global scope, regular functions, object methods, classes, arrow functions, and event handlers.
Understanding this is essential for working with objects, DOM events, and advanced patterns like method chaining and callbacks.
this refers to the window object in browsers.this depends on how the function is called (default/global, as a method, or using call/apply/bind).this usually refers to the object that owns the method.this; they inherit it (lexical this) from the surrounding scope.this refers to the element that received the event.this using bind(), call(), or apply().There is no special syntax just for this; you simply use it inside functions or methods:
this is resolved using these rules (simplified):
this from the enclosing scope.obj.method() → this is obj.func():
this is the global object (window in browsers).this is undefined.call/apply/bind → this is whatever you pass explicitly.this is the element that received the event.In the global scope (non-strict mode), this refers to the window object.
console.log(this); // window (in browser, non-strict mode)
In regular functions, the value of this depends on how the function is called.
function show() {
console.log(this);
}
show(); // window (in non-strict mode), undefined (in strict mode)
When used in an object method, this usually refers to the object that owns the method.
const person = {
name: "Alice",
greet: function() {
console.log("Hello, " + this.name);
}
};
person.greet(); // Hello, Alice
thisArrow functions do not have their own this. They inherit it from the surrounding (lexical) scope. Using them as object methods can lead to unexpected behavior.
const person = {
name: "Bob",
greet: () => {
console.log("Hi, " + this.name);
}
};
person.greet(); // Hi, undefined (this is NOT the person object)
In DOM event handlers using regular functions, this refers to the element that received the event.
document.querySelector("button").addEventListener("click", function() {
console.log(this); // the button element
});
this Manually with bindYou can explicitly set this using bind(), call(), or apply().
function greet() {
console.log("Hello, " + this.name);
}
const user = { name: "Charlie" };
const boundGreet = greet.bind(user);
boundGreet(); // Hello, Charlie
this directly in the global scope (non-strict) shows the window object.show(), it behaves like a global call, so this is window (or undefined in strict mode).person.greet() makes this equal to person, so you can access this.name.this, so this.name is not "Bob" and results in undefined.this refers to the button that was clicked, so you can manipulate that specific element.bind() usage: greet.bind(user) permanently sets this to the user object for the bound function.this from the parent (outer) scope.bind(), call(), or apply() when you need manual control over what this should be.this in event listeners and nested functions; arrow functions can help keep the expected this.this as undefined, not window.this pointing to the object itself.this to access one of its properties.this in both, and compare the results in different contexts.bind() or call() to explicitly set this for a function and verify the behavior.this to see the element that triggered the event.this from a regular function to an arrow function and observe how the value of this changes.