Skip to content

The this Keyword

How this is determined by the call site: default, implicit, explicit, and new binding.

What you'll learn

  • The four binding rules for this
  • Default binding and the strict-mode undefined case
  • Implicit binding via the call-site object
  • Explicit binding with call, apply, and bind
  • new binding and lexical this in arrow functions

Concept

What Is this?

this is a special keyword that refers to the object that "owns" the current execution. Unlike in some languages, this in JavaScript is not fixed by where a function is defined — it is determined by how the function is called (the call site). Understanding the four binding rules is the key to predicting what this will be.

1. Default Binding

A plain function call like foo() has default binding. In non-strict mode, this is the global object (window in browsers, global in Node). In strict mode, this is undefined. This is the fallback when no other rule applies.

2. Implicit Binding

When a function is called as a method on an object (obj.foo()), this is that object. This is implicit binding. However, the binding is lost if you detach the method: const fn = obj.foo; fn(); now has default binding. This is the classic "lost this" bug when passing methods as callbacks.

3. Explicit Binding

You can force this with call, apply, and bind. fn.call(obj, arg1, arg2) and fn.apply(obj, [arg1, arg2]) call the function immediately with this set to obj. fn.bind(obj) returns a new function with this permanently bound — useful for event handlers and callbacks that would otherwise lose their this.

4. new Binding

When a function is called with new, a new object is created and this is bound to it. This is how constructors work. The new keyword overrides all other binding rules.

Arrow Functions: Lexical this

Arrow functions do not follow these rules. They have no this of their own and inherit it from the surrounding lexical scope. This makes them ideal for callbacks inside methods where you want this to stay bound to the outer method's object. When you need a function's own this (object methods, constructors), use a regular function.

Example

              // 1. Default binding — 'this' is undefined in strict mode
function showThis() {
  "use strict";
  console.log("default:", this);
}
showThis(); // undefined (strict mode)

// 2. Implicit binding — 'this' is the object before the dot
const counter = {
  count: 0,
  increment() {
    this.count++;
    return this.count;
  },
};
console.log("implicit:", counter.increment()); // 1

// Lost 'this' — detaching the method breaks implicit binding
const detached = counter.increment;
try {
  detached(); // TypeError in strict mode
} catch (e) {
  console.log("lost this:", e.message);
}

// 3. Explicit binding with call, apply, and bind
function greet(greeting, punct) {
  return `${greeting}, ${this.name}${punct}`;
}
const person = { name: "Ada" };
console.log(greet.call(person, "Hello", "!"));   // "Hello, Ada!"
console.log(greet.apply(person, ["Hi", "."]));    // "Hi, Ada."
const boundGreet = greet.bind(person, "Hey");
console.log(boundGreet("?")); // "Hey, Ada?"

// 4. Arrow function keeps lexical 'this' inside a method
const timer = {
  label: "Timer",
  start() {
    setInterval(() => {
      console.log(this.label, "tick"); // 'this' is timer, not the interval
    }, 2000);
  },
};
            

Demonstrates all four binding rules: default (strict-mode undefined), implicit (method call), the lost-this bug, explicit binding with call/apply/bind, and lexical this in an arrow function callback.

Try it

  • Math Evaluator

    Methods that use this often perform calculations — test the logic.

  • Regex Tester

    Regex methods like test and exec depend on this binding.

Common mistakes

The mistake

Losing this when passing a method as a callback

The fix

const fn = obj.method detaches the method and this becomes undefined or the global. Use fn = obj.method.bind(obj), an arrow wrapper () => obj.method(), or method shorthand in a class.

The mistake

Using an arrow function for a method that needs its own this

The fix

Arrow functions inherit this from the surrounding scope and cannot be rebound. For object methods and class methods that rely on this, use regular function syntax or method shorthand.

Related Cheatsheets

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors