← Back to Chapters

JavaScript this Keyword

? JavaScript this Keyword

⚡ Quick Overview

The 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.

? Key Concepts

  • Global context (non-strict mode): this refers to the window object in browsers.
  • Regular functions: this depends on how the function is called (default/global, as a method, or using call/apply/bind).
  • Object methods: Inside a method, this usually refers to the object that owns the method.
  • Arrow functions: Do not have their own this; they inherit it (lexical this) from the surrounding scope.
  • Event handlers: In DOM event listeners (with regular functions), this refers to the element that received the event.
  • Manual control: You can control this using bind(), call(), or apply().

? Syntax and Theory

There is no special syntax just for this; you simply use it inside functions or methods:

this is resolved using these rules (simplified):

  1. If used inside an arrow function → inherit this from the enclosing scope.
  2. If called as a method obj.method()this is obj.
  3. If called as a plain function func():
    • Non-strict mode → this is the global object (window in browsers).
    • Strict mode → this is undefined.
  4. If used with call/apply/bindthis is whatever you pass explicitly.
  5. In event listeners (regular functions) → this is the element that received the event.

? Code Examples

? Global Context

In the global scope (non-strict mode), this refers to the window object.

? View Code Example
console.log(this); // window (in browser, non-strict mode)

? Inside Regular Functions

In regular functions, the value of this depends on how the function is called.

? View Code Example
function show() {
console.log(this);
}

show(); // window (in non-strict mode), undefined (in strict mode)

? Inside Object Methods

When used in an object method, this usually refers to the object that owns the method.

? View Code Example
const person = {
name: "Alice",
greet: function() {
console.log("Hello, " + this.name);
}
};

person.greet(); // Hello, Alice

⚠️ Arrow Functions and this

Arrow 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.

? View Code Example
const person = {
name: "Bob",
greet: () => {
console.log("Hi, " + this.name);
}
};

person.greet(); // Hi, undefined (this is NOT the person object)

?️ Event Handlers

In DOM event handlers using regular functions, this refers to the element that received the event.

? View Code Example
document.querySelector("button").addEventListener("click", function() {
console.log(this); // the button element
});

? Controlling this Manually with bind

You can explicitly set this using bind(), call(), or apply().

? View Code Example
function greet() {
console.log("Hello, " + this.name);
}

const user = { name: "Charlie" };

const boundGreet = greet.bind(user);
boundGreet(); // Hello, Charlie

? Live Output and Explanation

? What the Examples Show

  • Global example: Logging this directly in the global scope (non-strict) shows the window object.
  • Regular function: Called as show(), it behaves like a global call, so this is window (or undefined in strict mode).
  • Object method: person.greet() makes this equal to person, so you can access this.name.
  • Arrow method: The arrow function does not bind its own this, so this.name is not "Bob" and results in undefined.
  • Event handler: Inside the click listener, 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.

? Helpful Tips

  • Use arrow functions when you want to inherit this from the parent (outer) scope.
  • Use bind(), call(), or apply() when you need manual control over what this should be.
  • Be careful when using this in event listeners and nested functions; arrow functions can help keep the expected this.
  • In strict mode, a regular function called without an owner will have this as undefined, not window.
  • Avoid using arrow functions as object methods if you rely on this pointing to the object itself.

? Practice Tasks

  • Create an object with a method that uses this to access one of its properties.
  • Write a regular function and an arrow function, log this in both, and compare the results in different contexts.
  • Use bind() or call() to explicitly set this for a function and verify the behavior.
  • Add a click event listener to a button and log this to see the element that triggered the event.
  • Refactor a callback that uses this from a regular function to an arrow function and observe how the value of this changes.