# this Keyword & Binding Rules

> Explain how 'this' works in JavaScript. What are the binding rules and how do arrow functions differ?

Canonical: https://www.internhack.in/learn/interview/javascript-interview/js-this-keyword
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

Explain how 'this' works in JavaScript. What are the binding rules and how do arrow functions differ?

## Answer

The 'this' keyword in JavaScript is determined by how a function is called, not where it is defined. There are four binding rules in order of precedence:

1. new binding: When a function is called with 'new', this refers to the newly created object.
2. Explicit binding: call(), apply(), or bind() explicitly set this.
3. Implicit binding: When a function is called as a method of an object, this refers to that object.
4. Default binding: In non-strict mode, this defaults to the global object (window/global). In strict mode, this is undefined.

Arrow functions are a special case - they do not have their own 'this'. Instead, they inherit 'this' from the enclosing lexical scope at the time they are defined. This makes them ideal for callbacks and event handlers where you want to preserve the outer 'this'.

**Resolve this, rule by rule. Pick a call style and watch the binding rules decide. Turn on Predict to guess first.**

1. **Arrow function?**: Arrows inherit this from the surrounding scope.
2. **new binding**: new makes a fresh object and binds this to it.
3. **Hard binding with bind()**: A bound function always uses the bound object, even if called with call().
4. **Explicit binding with call / apply**: this is the first argument.
5. **Implicit binding**: obj.method() sets this to obj, the object left of the dot.
6. **Default binding**: A plain call gets undefined in strict mode or globalThis in sloppy mode, which is why const g = user.greet; g() loses this.

## Code examples

### The Four Binding Rules

```
// 1. Default binding
function showThis() {
  'use strict';
  console.log(this);
}
showThis(); // undefined (strict mode)

// 2. Implicit binding
const obj = {
  name: 'Alice',
  greet() {
    console.log(`Hello, ${this.name}`);
  }
};
obj.greet(); // Hello, Alice

// 3. Explicit binding
const bob = { name: 'Bob' };
obj.greet.call(bob); // Hello, Bob

// 4. new binding
function Person(name) {
  this.name = name;
}
const p = new Person('Charlie');
console.log(p.name); // Charlie
```

Binding precedence: new > explicit (call/apply/bind) > implicit (method call) > default (standalone call).

### Common 'this' Pitfall

```
const user = {
  name: 'Alice',
  friends: ['Bob', 'Charlie'],
  showFriends() {
    // 'this' is user here
    this.friends.forEach(function(friend) {
      // 'this' is undefined/window here (not user!)
      console.log(`${this?.name} knows ${friend}`);
    });
  },
  showFriendsFixed() {
    // Arrow function inherits 'this' from showFriendsFixed
    this.friends.forEach((friend) => {
      console.log(`${this.name} knows ${friend}`);
    });
  }
};

user.showFriends();
console.log('---');
user.showFriendsFixed();
```

Regular functions in callbacks lose the outer 'this'. Arrow functions solve this by inheriting from the enclosing scope.

### Arrow Functions and this

```
const timer = {
  seconds: 0,
  // Arrow function - inherits 'this' from timer object context
  start() {
    // 'this' is timer here
    const tick = () => {
      this.seconds++; // 'this' is still timer
      console.log(this.seconds);
    };
    tick();
    tick();
  }
};
timer.start();

// Arrow functions cannot be used as constructors
const Foo = () => {};
try {
  new Foo();
} catch (e) {
  console.log(e.message);
}

// Arrow function 'this' cannot be overridden
const arrowFn = () => console.log(this);
arrowFn.call({ a: 1 }); // ignores call, uses lexical this
```

Arrow functions have no own 'this', no 'arguments', no 'prototype', and cannot be used with 'new'. Their 'this' is permanently set at creation time.

## Key points

- Arrow functions do not have their own this, arguments, super, or new.target
- In event handlers, 'this' refers to the element that triggered the event (unless using arrow functions)
- bind() returns a new function with 'this' permanently set - it cannot be overridden

## Common follow-up questions

- What does 'this' refer to in a class method vs a regular function?
- How does 'this' work in event listeners?
- Can you explain why setTimeout callbacks lose 'this'?

## Interview tips

- Draw a decision tree: new > call/apply/bind > obj.method > standalone
- The forEach callback losing 'this' is the most common practical example
- Always mention arrow functions as the modern solution to 'this' confusion

## Concepts covered

this, Implicit Binding, Explicit Binding, Arrow Functions, Global Context

Previous: [Object Methods & Property Descriptors](https://www.internhack.in/learn/interview/javascript-interview/js-object-methods) · Next: [call, apply, bind](https://www.internhack.in/learn/interview/javascript-interview/js-call-apply-bind) · [All of JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)

## More JavaScript Interview questions

- [Prototypes & Prototype Chain](https://www.internhack.in/learn/interview/javascript-interview/js-prototypes)
- [Classes & Inheritance](https://www.internhack.in/learn/interview/javascript-interview/js-classes-inheritance)
- [Promises Deep Dive](https://www.internhack.in/learn/interview/javascript-interview/js-promises-deep)
- [async/await Patterns](https://www.internhack.in/learn/interview/javascript-interview/js-async-await)
- [Type Coercion & Comparison](https://www.internhack.in/learn/interview/javascript-interview/js-type-coercion)
- [Modules (CommonJS vs ESM)](https://www.internhack.in/learn/interview/javascript-interview/js-modules)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [JavaScript Interview](https://www.internhack.in/learn/interview/javascript-interview)
