What is an arrow function?
Arrow functions, introduced in ES6. An arrow function is a shorter way to write JavaScript functions.
Vivek Rastogi
8/20/20251 min read
Why do we use arrow functions?
Shorter syntax – Less typing, cleaner code.
Automatic this binding – Arrow functions don’t have their own this.
In normal functions, this changes based on how you call the function.
In arrow functions, this stays the same as the surrounding context.
Instead of writing:
function greet(name) {
return "Hello, " + name;
}
You can write:
const greet = (name) => {
return "Hello, " + name;
};
Advantages
Cleaner and more readable code
// Regular function
const numbers = [1, 2, 3];
const squares = numbers.map(function(n) {
return n * n;
});
// Arrow function
const squares = numbers.map(n => n * n);