Facebook Pixel

this in Arrow Functions vs Regular Functions

Arrow functions inherit this; regular functions get this at call time. Here is the difference and the bugs it causes.

this in Arrow Functions vs Regular Functions

The biggest difference between arrow functions and regular functions is how they handle this. Getting this wrong causes real bugs.

Regular Functions: this at Call Time

A regular function's this is determined by how it is called:

function Foo() { this.value = 1; setTimeout(function () { console.log(this.value); // undefined (this is window/undefined) }, 100); } new Foo();

The callback is a plain call, so this is the global object (or undefined in strict mode). It does not inherit from Foo.

Arrow Functions: this at Definition Time

Arrows do not have their own this. They inherit from the enclosing lexical scope:

function Foo() { this.value = 1; setTimeout(() => { console.log(this.value); // 1 (this is the Foo instance) }, 100); } new Foo();

The arrow captures this from Foo's scope, where this is the instance.

Common Bug: Method Defined as Arrow

const obj = { name: "Kunal", greet: () => console.log(this.name), }; obj.greet(); // undefined

The arrow's this is the enclosing scope's this (the global/module scope), not obj. For methods, use regular functions:

const obj = { name: "Kunal", greet() { console.log(this.name); }, // regular function }; obj.greet(); // "Kunal"

Common Bug: Method Defined as Regular, Used as Callback

const obj = { name: "Kunal", greet() { console.log(this.name); }, }; setTimeout(obj.greet, 100); // undefined (this is lost)

Fix with bind or an arrow wrapper:

setTimeout(obj.greet.bind(obj), 100); // "Kunal" setTimeout(() => obj.greet(), 100); // "Kunal"

Rule of Thumb

  • Object methods: use regular functions (so this is the object).
  • Callbacks inside methods/constructors: use arrows (so this is preserved).
  • Top-level functions: use whatever you prefer; this is rarely needed.

The Takeaway

Regular functions get this at call time (from how they are invoked). Arrows inherit this lexically from where they are written. Use regular functions for object methods, arrows for callbacks inside methods.

Regular functions get this at call time based on how they are invoked. Arrow functions do not have their own this; they inherit it from the enclosing lexical scope at definition time.

Because an arrow function's this is the enclosing scope's this, not the object. So obj.greet = () => this.name gives undefined. Use regular functions for methods so this is the object.

Because arrows inherit this from their lexical scope. An arrow inside a method or constructor captures this from that scope, so it still refers to the instance inside the callback.

No. Arrow functions do not have their own this binding. bind, call, and apply cannot change an arrow's this. They can still pass arguments, but this stays lexical.

Use regular functions for object methods so this is the object. Use arrow functions for callbacks inside methods or constructors so this is preserved from the enclosing scope.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.