JavaScript Arrow Functions
Arrow functions (ES2015) are a shorter syntax for writing function expressions. They're perfect for small callbacks — the kind you pass to map, filter, sort or addEventListener — and they handle this differently from regular functions.
From function to arrow
// function expression
const double1 = function (n) {
return n * 2;
};
// arrow function with a block body
const double2 = (n) => {
return n * 2;
};
// arrow function with an expression body: implicit return
const double3 = (n) => n * 2;
console.log(double1(4), double2(4), double3(4));8 8 8
Syntax variations
| Form | Example |
|---|---|
| no parameters | () => 42 |
| one parameter (parentheses optional) | x => x * 2 or (x) => x * 2 |
| several parameters | (a, b) => a + b |
block body (needs return) |
(a, b) => { const s = a + b; return s; } |
| returning an object literal | () => ({ ok: true }) |
| default / rest parameters | (x = 1, ...more) => … |
The expression body form (no braces) returns the expression's value automatically. As soon as you add { }, you must write return yourself.
const greet = () => "Hello!";
const add = (a, b) => a + b;
const toPoint = (x, y) => ({ x, y }); // parentheses around the object!
const broken = (x, y) => { x, y }; // braces = block body, returns undefined
console.log(greet(), add(2, 3));
console.log(toPoint(1, 2));
console.log(broken(1, 2));Hello! 5
{ x: 1, y: 2 }
undefined
Arrow functions as callbacks
This is where arrows shine — compact, readable data pipelines:
const products = [
{ name: "Desk", price: 180, category: "office" },
{ name: "Lamp", price: 35, category: "home" },
{ name: "Chair", price: 95, category: "office" },
];
const officeNames = products
.filter((p) => p.category === "office")
.map((p) => p.name.toUpperCase());
const cheapestFirst = [...products].sort((a, b) => a.price - b.price).map((p) => p.name);
console.log(officeNames);
console.log(cheapestFirst);
console.log(products.reduce((sum, p) => sum + p.price, 0));[ 'DESK', 'CHAIR' ]
[ 'Lamp', 'Chair', 'Desk' ]
310
Arrows and this
An arrow function has no this of its own. It uses the this from the surrounding code. Inside a method, that means callbacks can still reach the object:
const playlist = {
title: "Road trip",
songs: ["Drive", "Highway", "Sunset"],
describe() {
return this.songs.map((song, i) => `${this.title} #${i + 1}: ${song}`);
},
};
console.log(playlist.describe());[
'Road trip #1: Drive',
'Road trip #2: Highway',
'Road trip #3: Sunset'
]
With a regular function callback, this inside it would be undefined and the code would break.
When not to use arrow functions
Because they lack their own this, arguments and constructor ability, arrows are the wrong choice in a few places:
const person = {
name: "Ivo",
arrowHello: () => `Hi, I'm ${this?.name}`, // this is NOT person
methodHello() {
return `Hi, I'm ${this.name}`;
},
};
console.log(person.arrowHello());
console.log(person.methodHello());
const Car = (brand) => {
this.brand = brand;
};
try {
new Car("Fiat");
} catch (err) {
console.log(err.message);
}Hi, I'm undefined
Hi, I'm Ivo
Car is not a constructor
| Use an arrow function for | Use a regular function / method for |
|---|---|
callbacks (map, filter, then, timers) |
object methods that use this |
| short helper functions | constructors (or better: classes) |
keeping the outer this inside a method |
event handlers that need this = element |
functions that use arguments |
Note: Arrow functions are not hoisted like function declarations — they're values stored in variables, so define them before you call them.
Test yourself
What does const f = x => x + 1; f(4) return?
- 4
- 5
- undefined
- "x + 1"
What does const f = () => { value: 1 }; f() return?
- { value: 1 }
- 1
- undefined
- A SyntaxError
Why shouldn't you use an arrow function as an object method that uses this?
- Arrow functions can't take parameters
- Arrows don't get the object as their own this
- Arrows are slower
- Arrows can't return values