Arrow functions are a shorter way to write functions in JavaScript, introduced in ES6 (ECMAScript 2015).
They make code cleaner and automatically handle the this keyword differently compared to regular functions.
1️⃣ Syntax Difference
Regular Function:
function add(a, b) {
return a + b;
}
Arrow Function:
const add = (a, b) => a + b;
✅ Shorter syntax
✅ Implicit return (no need for return keyword when one line)
2️⃣ Key Differences
| Feature | Regular Function | Arrow Function |
|---|---|---|
| Syntax | Longer | Shorter and cleaner |
this binding |
Has its own this |
Inherits this from parent scope (lexical this) |
arguments object |
Available | Not available |
Constructor (new) |
Can be used as a constructor | ❌ Cannot be used as a constructor |
prototype |
Has a prototype property |
❌ No prototype |
3️⃣ Example of this Difference
Regular Function:
function Person() {
this.age = 0;
setInterval(function() {
this.age++; // 'this' refers to window/global object
}, 1000);
}
new Person();
🧨 Here, this loses its reference — it points to the global object, not Person.
Arrow Function Fix:
function Person() {
this.age = 0;
setInterval(() => {
this.age++; // 'this' correctly refers to Person
}, 1000);
}
new Person();
✅ Arrow function inherits this from its parent scope (lexical scope).
4️⃣ When to Use Arrow Functions
- Inside callbacks or event handlers.
- When you want to preserve the parent scope’s
this. - When defining small, inline utility functions.
⚡ In short:
Arrow functions are concise and automatically use lexical
this, while regular functions have their ownthisand can be used as constructors.