The this Keyword

How JavaScript decides what this refers to — the four binding rules (default, implicit, explicit, new), arrow functions, call/apply/bind, lost-this bugs with callbacks, and this in classes.

Intermediate 2 min readLesson 4 of 12 Interview Q&A

In most languages, this is simply "the current object". In JavaScript, this is determined by how a function is called, not where it's defined — with one big exception: arrow functions. Four rules, checked in priority order, cover every case.

The four rules (highest priority first)

RuleCall formthis is
1. new bindingnew Foo()The newly created object
2. Explicit bindingfn.call(obj), fn.apply(obj), fn.bind(obj)()obj
3. Implicit bindingobj.fn()obj (the object before the dot)
4. Default bindingfn()undefined in strict mode/modules; the global object in sloppy mode

Arrow functions ignore all four: they have no this of their own and use the this of the enclosing scope (lexical this).

Implicit binding — and how it gets lost

JavaScript
const user = {
  name: "Ada",
  greet() { return `Hi, I'm ${this.name}`; },
};

user.greet();                    // "Hi, I'm Ada" — this = user

const greet = user.greet;        // copy the function reference
greet();                         // TypeError (strict): this is undefined — the object is gone

setTimeout(user.greet, 100);     // same problem: setTimeout calls it as a plain function
button.addEventListener("click", user.greet);   // this = the button element!

this isn't stored in the function. Taking user.greet produces just the function; whoever calls it decides this.

Explicit binding: call, apply, bind

JavaScript
function introduce(greeting, punctuation) {
  return `${greeting}, I'm ${this.name}${punctuation}`;
}
const ada = { name: "Ada" };

introduce.call(ada, "Hello", "!");      // args individually
introduce.apply(ada, ["Hello", "!"]);   // args as an array
const bound = introduce.bind(ada, "Hey");   // returns a NEW function with this fixed (and partial args)
bound("?");                              // "Hey, I'm Ada?"

setTimeout(user.greet.bind(user), 100);  // fix the lost-this problem

A bound function's this can't be changed again by call/apply/bind (only new overrides it).

new binding

JavaScript
function Person(name) {
  // 1. a new object is created, linked to Person.prototype
  // 2. this = that object
  this.name = name;
  // 3. it's returned automatically (unless you return another object)
}
const p = new Person("Ada");

Arrow functions: lexical this

Arrow functions capture this from where they're defined, which makes them ideal for callbacks inside methods:

JavaScript
class Timer {
  seconds = 0;
  start() {
    setInterval(() => {
      this.seconds++;          // arrow: 'this' is the Timer instance from start()
    }, 1000);
  }
  startBroken() {
    setInterval(function () {
      this.seconds++;          // regular function: 'this' is undefined (or the global object)
    }, 1000);
  }
}

But arrow functions are wrong as object methods when you need the object:

JavaScript
const counter = {
  count: 0,
  inc: () => this.count++,     // 'this' is the module/global scope's this, NOT counter
};

Arrows also can't be used with new, and call/bind can't change their this.

this in classes

Class bodies are strict mode, so lost methods get undefined:

JavaScript
class Button {
  constructor(label) { this.label = label; }
  handleClick() { console.log(this.label); }
}
const b = new Button("Save");
document.querySelector("#save").addEventListener("click", b.handleClick);   // logs undefined (this = element)... or throws

// Fix 1: bind in the constructor
class Button1 {
  constructor(label) { this.label = label; this.handleClick = this.handleClick.bind(this); }
  handleClick() { console.log(this.label); }
}

// Fix 2: class field with an arrow function (one copy per instance)
class Button2 {
  label = "Save";
  handleClick = () => console.log(this.label);
}

// Fix 3: wrap at the call site
el.addEventListener("click", () => b.handleClick());

This is why older React class components had this.handleClick = this.handleClick.bind(this) everywhere.

Quick quiz

JavaScript
"use strict";
const obj = {
  name: "obj",
  regular() { return this?.name; },
  arrow: () => this?.name,
  nested() {
    const inner = function () { return this?.name; };
    const innerArrow = () => this?.name;
    return [inner(), innerArrow()];
  },
};

obj.regular();               // "obj"            implicit
obj.arrow();                 // undefined        lexical (module scope)
obj.nested();                // [undefined, "obj"]
const r = obj.regular; r();  // undefined        default (strict)
obj.regular.call({ name: "x" });   // "x"        explicit

Decision procedure

  1. Is it an arrow function? Use this from the enclosing scope. Done.
  2. Called with new? this is the new object.
  3. Called with call/apply, or is it a bound function? this is the given object.
  4. Called as obj.method()? this is obj.
  5. Otherwise: undefined in strict code (modules and classes are strict), the global object in sloppy scripts.
Key takeaways
  • this depends on the call site, not the definition — except for arrow functions.
  • Priority: new > explicit (call/apply/bind) > implicit (obj.fn()) > default.
  • Passing obj.method as a callback loses this; fix with bind, an arrow wrapper, or an arrow class field.
  • Arrow functions capture this lexically: great for callbacks, wrong for object methods.
  • Modules and classes are strict mode: default this is undefined.
Interview questions
QWhat is the difference between call, apply and bind?easy

All three set this explicitly. call invokes the function immediately with arguments listed individually; apply invokes it immediately with arguments as an array; bind doesn't invoke it but returns a new function with this (and optionally leading arguments) permanently fixed.