Arrow functions provide a concise syntax to write functions in JavaScript. However, their behavior with this is different from regular functions, which is essential to understand for correct usage.

💡 Key Point
Arrow functions do not have their own this. Instead, they inherit this from the surrounding (lexical) context where they are defined.
Regular Functions: this depends on the caller
In regular functions, this is dynamic and depends on how the function is called:
📌 Deep Dive: Regular function this
const obj = {
value: 42,
regularFunc: function() {
console.log(this.value);
}
};
obj.regularFunc(); // Logs: 42
const detached = obj.regularFunc;
detached(); // Logs: undefined (or global value depending on environment)
undefined
Arrow Functions: this is lexical
Arrow functions capture the this value from their surrounding scope at the time they are created, ignoring how they are called later.
📌 Deep Dive: Arrow function this
const obj = {
value: 42,
arrowFunc: () => {
console.log(this.value);
}
};
obj.arrowFunc(); // Logs: undefined (since arrowFunc's this is not obj)
In the above example, this inside arrowFunc is inherited from the outer scope, which is usually the global or module scope, not obj.
⚠️ Common Pitfall
Do not use arrow functions as methods in objects if you rely on this referring to the object itself.
When Arrow Functions Shine with this
- Inside callbacks: Arrow functions preserve the
thisof the enclosing scope, useful to avoidvar self = thistricks. - In class methods or event handlers: to keep
thisbound to the instance.
📌 Deep Dive: Using arrow function in a callback
function Timer() {
this.seconds = 0;
setInterval(() => {
this.seconds++;
console.log(this.seconds);
}, 1000);
}
const timer = new Timer();
// Logs incrementing seconds every second
2
3
...
this behavior| Function Type | this Behavior |
|---|---|
| Regular function | Dynamic, depends on caller |
| Arrow function | Lexical, fixed to outer scope |
💡 Summary
- Arrow functions do not bind their own
this. - Use arrow functions when you want to inherit
thisfrom the surrounding scope. - Avoid arrow functions as object methods if you need
thisto refer to the object.
Quick Knowledge Check
Test what you just learned
Question 1 of 2
How is the value of this determined within the context of an arrow function?
Question 2 of 2
Why should you avoid using arrow functions as object methods?
Loading results...