← Back to JavaScript, page 1

javascript-0004 · Medium mid

How is the value of this decided in JavaScript?

For regular functions, this is decided by how the function is called, not where it is written. A method call uses the object before the dot, new uses the new object, call, apply and bind set it explicitly, and a plain call gives undefined in strict mode. Arrow functions have no this of their own and use the one from the surrounding scope.

Full answer

this is a hidden parameter that every regular function receives at call time. To know its value, look at the call site and apply these rules in order.

  1. If the function is called with new, this is the freshly created object.
  2. If it is called through call, apply, or a function made by bind, this is the object given there.
  3. If it is called as a method, such as student.greet(), this is the object left of the dot.
  4. Otherwise it is a plain call. In strict mode, which includes modules and classes, this is undefined. In sloppy mode it falls back to the global object.

Arrow functions do not follow these rules. They have no this of their own, so they read this from the scope where they were defined, and nothing can change it later, not even call.

The classic bug is detaching a method. Writing const f = student.greet copies only the function, so the call f() no longer has an object before the dot. The same thing happens when a method is passed as a callback.

Fixes include bind, wrapping the call in an arrow function, or defining the method as an arrow function in a class field.

javascript
'use strict';

const student = {
  name: 'Asha',
  greet() {
    return `Hi, ${this === undefined ? 'no this' : this.name}`;
  },
  greetLater() {
    return [1].map(() => this.name); // arrow keeps this of greetLater
  },
};

console.log(student.greet());

const detached = student.greet;
console.log(detached());

const bound = student.greet.bind({ name: 'Ravi' });
console.log(bound());
console.log(student.greet.call({ name: 'Meera' }));
console.log(student.greetLater());

function Course(title) {
  this.title = title;
}
const course = new Course('JavaScript');
console.log(course.title);

const arrow = () => typeof this;
console.log(arrow.call({ name: 'ignored' }));

In a CommonJS file the top-level this is module.exports, which is an object, so the last line prints object. The arrow function ignores call() and keeps that outer this. Run with Node.js v24.22.1 as js-4.js; two runs gave identical output. Output: Hi, Asha Hi, no this Hi, Ravi Hi, Meera [ 'Asha' ] JavaScript object

Key points

  • this depends on the call site for regular functions.
  • Order of precedence: new, explicit binding (call, apply, bind), method call, plain call.
  • A plain call gives undefined in strict mode and the global object in sloppy mode.
  • Arrow functions take this from the enclosing scope and ignore call, apply, and bind.
  • A method passed as a callback loses its object unless it is bound.

Common mistakes

  • Saying this refers to the function itself. It does not.
  • Using an arrow function as an object method and expecting this to be the object.
  • Forgetting that class bodies are always strict, so a detached class method gets undefined, not the global object.
  • Thinking bind changes the original function. It returns a new one, and a bound function cannot be rebound.

Follow-ups

  • Why does this.setState or this.name fail inside a setTimeout callback written with function()?
  • What is this at the top level of a CommonJS file compared with an ES module?
  • If an arrow function is called with call(obj), which this does it see?

Tips

  • Answer with the four-step order and one line of code per step. A clear list beats a long story.
  • Mention strict mode early; many wrong answers ignore it.
  • When shown a tricky snippet, name the call site first, then give the value of this.