Skip to content

Arrow Functions

An arrow function is a shorter way to write a function expression. It drops the function keyword and puts => between the parameters and the body. Arrow functions are most useful for short callbacks, such as the functions passed to array methods.

(parameter1, parameter2) => expression

From function expression to arrow function

Section titled “From function expression to arrow function”

The three functions below do the same thing. The first is a function expression, the second is the same function as an arrow function with a block body, and the third uses a concise body:

const toFahrenheitA = function (celsius) {
return celsius * 9 / 5 + 32;
};
const toFahrenheitB = (celsius) => {
return celsius * 9 / 5 + 32;
};
const toFahrenheitC = celsius => celsius * 9 / 5 + 32;
console.log(toFahrenheitA(20), toFahrenheitB(20), toFahrenheitC(20));

This prints:

68 68 68

A concise body is a single expression without braces, and its value is returned automatically. A block body has braces and needs an explicit return, as any other function does.

The parentheses can be left out only when there is exactly one simple parameter. No parameters, two or more parameters, a default value, a rest parameter or destructuring all need them:

const add = (a, b) => a + b;
const greet = () => 'Good morning';
console.log(add(2, 3));
console.log(greet());

This prints:

5
Good morning

Many teams keep the parentheses even for one parameter, because the code formatter Prettier adds them by default and adding a second parameter later then needs no other change.

Array methods such as filter, map and forEach take a function and call it once for each element. An arrow function keeps these calls short enough to read on one line:

const books = [
{ title: 'The Hobbit', pages: 310 },
{ title: 'Matilda', pages: 240 },
{ title: 'Middlemarch', pages: 880 },
];
const long = books.filter(book => book.pages > 300);
const titles = long.map(book => book.title);
console.log(titles);
books.forEach((book, index) => console.log(index + 1, book.title));

This prints:

[ 'The Hobbit', 'Middlemarch' ]
1 The Hobbit
2 Matilda
3 Middlemarch

forEach passes the element and its index, so the callback takes two parameters and needs parentheses. Array Filter Method, Array Map Method and Array ForEach Method cover the methods themselves.

Arrow functions are not only shorter. They also behave differently from functions written with function:

  • They have no this of their own. Inside an arrow function, this is the this of the surrounding code.
  • They have no arguments object, so use a rest parameter instead.
  • They cannot be called with new, because they are not constructors.

The first difference makes arrow functions a good fit for callbacks inside methods, because the callback sees the method’s this:

const timer = {
seconds: 0,
start() {
[1, 2, 3].forEach(() => {
this.seconds += 1;
});
console.log(this.seconds);
},
};
timer.start();

This prints:

3

With a function callback in place of the arrow function, this inside the callback would not be timer. The same difference makes arrow functions a poor choice for object methods themselves, which Method Shorthand shows. Tips for ES6 Arrow Functions collects the other common mistakes.

Steve Griffith covers this topic in his video ES6 JavaScript Arrow Functions.