# The 'this' Keyword & Binding

> Demystify JavaScript's most confusing keyword. Learn the rules that determine what 'this' points to at runtime.

Subject: JavaScript | Lesson 15 of 63 | 8 min read
Interactive lesson: https://codewithanimation.in/javascript/this-binding
Previous: [Scope Chain & Closures](https://codewithanimation.in/javascript/scope-and-closures.md) | Next: [Higher-Order Functions & Callbacks](https://codewithanimation.in/javascript/higher-order-functions.md)

## What is 'this'?

In many programming languages, `this` always points to the class or object where the method is written.

In JavaScript, `this` is determined by *how* a function is called, not *where* it is written. The same function can have a different `this` depending on the call site.

## The Four Rules of Binding

1. **Implicit Binding**: If a function is called as a method on an object (`user.greet()`), `this` points to that object (`user`).

2. **Explicit Binding**: Using `.call()`, `.apply()`, or `.bind()`, you can forcefully dictate what `this` should be.

3. **New Binding**: Calling a function with the `new` keyword (`new User()`) sets `this` to a brand new empty object.

4. **Default Binding**: A plain standalone function call (`greet()`) sets `this` to the global object (`window` in browsers) or `undefined` in strict mode.

## Arrow Functions break the rules

Arrow functions (`() => {}`) do not have their own `this` context. Instead, they use 'lexical binding'.

This means they inherit `this` from the parent scope at the exact moment they are defined, and you cannot change it later-even with `.call()` or `.bind()`.

## Common mistake: Losing 'this' in callbacks

The most common bug is passing a method as a callback (e.g., `setTimeout(user.greet, 1000)`). When `setTimeout` eventually runs the function, it calls it standalone, stripping the `user.` context. `this` falls back to the global object, and your code crashes! Fix it by passing an arrow function or using `.bind()`.

## Tip: The golden rule

Whenever you see the `this` keyword inside a standard `function`, immediately look at the code that *called* the function. Look to the left of the dot at the call site. That's your `this`!

## Try it: The Target Tracker

Toggle between different function invocation styles (Standalone, Method, Explicit, and Arrow) to see exactly what object the `this` keyword snaps to.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/this-binding

## Check yourself

### Question 1

In the call `player.jump()`, what will `this` point to inside the `jump` function?

- A. The player object
- B. The global window object
- C. undefined

Correct answer: A

Because `jump` is invoked as a property of `player` (Implicit Binding), the `this` context inside the function points directly to the `player` object.

- Why not B: Incorrect. The function was called as a method on `player`, so implicit binding takes over.
- Why not C: Incorrect. That only happens in strict mode with a standalone function call.

### Question 2

How does an Arrow Function determine its `this` value?

- A. It inherits `this` lexically from the surrounding scope where it was defined.
- B. It looks at the object to the left of the dot at call time.
- C. It always binds to the global object.

Correct answer: A

Arrow functions use lexical scoping for `this`. They do not create their own context; they simply adopt the `this` of the enclosing block where they were written.

- Why not B: Incorrect. That is how standard `function` declarations work.
- Why not C: Incorrect. It inherits from its parent scope, which might be another object or class.

### Question 3

What happens if you run `const fn = user.greet; fn();` in strict mode?

- A. this is undefined
- B. this is the user object
- C. this is the global window object

Correct answer: A

By storing the method in a variable (`fn`) and calling it without a dot (`fn()`), it becomes a standalone function call. Default binding applies, which yields `undefined` in strict mode.

- Why not B: Incorrect. You extracted the function, stripping away the `user.` context before calling it.
- Why not C: Incorrect. In strict mode, default binding falls back to `undefined`, not `window`.

## Remember this

- For standard functions, `this` is determined by HOW the function is called, not where it is written.
- Look to the left of the dot at call time: `obj.method()` binds `this` to `obj`.
- Standalone calls (`fn()`) bind `this` to the global object (or `undefined` in strict mode).
- Arrow functions ignore all rules and inherit `this` lexically from their surrounding scope.
