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) => expressionFrom 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 68A 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.
Parentheses around the parameters
Section titled “Parentheses around the parameters”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:
5Good morningMany 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.
Arrow functions as callbacks
Section titled “Arrow functions as callbacks”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 Hobbit2 Matilda3 MiddlemarchforEach 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.
How arrow functions differ
Section titled “How arrow functions differ”Arrow functions are not only shorter. They also behave differently from functions written with function:
- They have no
thisof their own. Inside an arrow function,thisis thethisof the surrounding code. - They have no
argumentsobject, 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:
3With 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.