Facebook Pixel

When should you use a regular function vs an arrow function for this in JavaScript?

Use regular functions for object methods (this should be the object). Use arrow functions for callbacks inside methods or constructors (this is preserved from the enclosing scope).

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in this in Arrow Functions vs Regular Functions in JavaScript

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 their 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.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.