Why Is `this` So Confusing ?

The reason is simple because JavaScript allows a function to be called in many different ways. A function can be called as an object method, as a normal function, with `new`, with `call()` or `apply()`, or as a callback. These different call patterns can produce different `this` values.

 

The Most Important Idea

For a regular function, the value of `this` is mainly decided by how the function is called, not simply by where the function is written.

const user = {
    name: "Rahul",
    greet() {
        console.log(this.name);
    }
};

user.greet();  // Rahul

Here, `greet()` is called `user.greet()`. The object before the dot is `user`, so `this` refers to that object during this call.

 

A Better Question

Instead of asking, “Where was this function written?”, ask, “How is this function being called right now?” This small change in thinking makes many `this` problems much easier.

 

1. What Is `this` ?

  • `this` is a special keyword provided by JavaScript. It is not a normal variable. You do not create it with `let`, `const`, or `var`, and you cannot assign a value to it directly.
  • A useful way to think about `this` is as a value that JavaScript makes available while a function is running. For regular functions, the way the function is invoked is a major part of deciding that value.

 

2. `this` with Standalone Function Call

function showThis() {
    "use strict";
    console.log(this);  }

showThis(); // undefined

In strict mode, a regular function called without a receiver gets `undefined` as `this`. In older non-strict code, JavaScript may use the global object instead.

 

3. Simple Checklist

  • Is this a regular function or an arrow function?
  • Is it being called with `new`?
  • Was it called using `call()` or `apply()`?
  • Was it previously fixed with `bind()`?
  • If not, is it being called `object.method()`?

 

4. Arrow Functions: Why Their `this` Is Different

Arrow functions are one of the most important parts of the `this` topic. The key rule is simple: an arrow function does not create its own `this`.

const user = {
    name: "Rahul",
    showName() {
        const print = () => {
            console.log(this.name);

        };
    print();
    } };

user.showName();  // RahulThe regular method receives `user` as its `this`. The arrow function does not create a new `this`, so it uses the surrounding `this` from `showName()`.

 

This Is Called Lexical `this`

“Lexical” means that the arrow function gets `this` from where it is created in the surrounding code, rather than creating a new dynamic `this` when it is called.

 

Note - Regular function → its `this` can be determined by the call. Arrow function → no own `this`; it inherits `this` from the surrounding scope.

 

5. `this` with `new`, Classes, and Real Objects

The `new` keyword creates a new object and calls a constructor with that new object as `this`. This is the foundation of constructor functions and JavaScript class instances.

Constructor Function

function Employee(name, department) {
    this.name = name;
    this.department = department;
}

const employee = new Employee("Neha", "Engineering");
console.log(employee.name);      // Neha

 

Inside `Employee`, `this.name` creates a property on the newly created employee object.

 

The Same Idea with a Class

class Employee {
  constructor(name, department) {
      this.name = name;
      this.department = department;
  }
  introduce() {
    return `${this.name} works in ${this.department}`;
  }
}
 

const employee = new Employee("Neha", "Engineering");
console.log(employee.introduce());

 

In a class, `this` represents the current instance when an instance method is called normally.

6. Real-World Example: Shopping Cart

class ShoppingCart {
  constructor() {
      this.items = [ ];
  }
  addItem(item) {
        this.items.push(item);
  }
    getItemCount() {
        return this.items.length;
  }
}
const cart = new ShoppingCart();
cart.addItem("Laptop");
cart.addItem("Mouse");
console.log(cart.getItemCount());  // 2

 

Here, `this.items` represents the state of the current cart. Every new `ShoppingCart` object gets its own `items` array.

 

Real-world meaning

In classes, `this` usually means “this particular object instance.” It lets methods read and update the instance's own data.

7. Common Mistakes

  • Thinking `this` always means the object where the function was written.
  • Using an arrow function when a dynamic method `this` is required.
  • Forgetting that passing a method as a callback can remove its receiver.
  • Ignoring strict mode when testing standalone regular functions.
  • Trying to use `call()`, `apply()`, or bind()` to change an arrow function's `this`.

 

Debugging tip

When `this` gives an unexpected value, inspect the exact line that calls the function. The call site often explains the problem immediately.


 

8. Final Mental Model: How to Understand `this`

You do not need to memorize dozens of special cases. A small decision process is enough for most JavaScript code.

Step 1: Is It an Arrow Function?

If yes, it does not have its own `this`. Look at the surrounding scope and find the `this` that the arrow function inherits.

Step 2: Is It a Regular Function?

If yes, inspect how it is called. Look for `new`, `call()`, `apply()`, `bind()`, or an object method call.

Step 3: Look at the Call Site

user.greet();                     // method call
greet.call(user);               // explicit this
greet.apply(user);          // explicit this
const fn = greet.bind(user);
new Student("A");          // constructor call
greet();                            // standalone call


 

9. The Three Rules Worth Remembering

   Rule                                                            Meaning

Regular function

Check how it was called.

Arrow function

Uses `this` from the surrounding lexical scope.

`new` / `call` / `apply` / `bind`

These forms can explicitly determine the function's context.


 

10. Conclusion

Do not ask only “What does `this` mean?” Ask “What kind of function is this, and how is it being called?” Once you make that habit, `this` becomes predictable instead of mysterious.