Skip to content

Tips and Gotchas for ES6 Arrow Functions

Arrow functions are short, and the short forms have rules that are easy to trip over. Most mistakes come from the braces, which mean a function body in one place and an object literal in another. This page goes through the common ones. Arrow Functions introduces the syntax.

Without braces, the arrow function returns the value of its expression. With braces, the body is a block, and a block returns nothing unless it contains a return:

const doubleA = n => { n * 2 };
const doubleB = n => { return n * 2; };
const doubleC = n => n * 2;
console.log(doubleA(4), doubleB(4), doubleC(4));

This prints:

undefined 8 8

doubleA calculates n * 2 and then discards it. This is the most common arrow function bug, and it shows up as a map that returns an array of undefined.

An arrow function with no parameters needs an empty pair of parentheses. Some code uses _ instead, which looks like an empty list but is really a parameter named _ that the function ignores:

const noParams = () => 'ready';
const underscore = _ => 'ready';
console.log(noParams(), underscore());
console.log(noParams.length, underscore.length);

This prints:

ready ready
0 1

The length property counts the declared parameters, so the two functions are not quite the same. Use () when a function takes nothing, and keep _ for a parameter that must be declared but is not used.

An opening brace straight after => always starts a block body, so an object literal written there is not read as an object. To return an object from a concise body, wrap it in parentheses:

const makeItemA = name => { name: name };
const makeItemB = name => ({ name: name });
const makeItemC = name => ({ name, done: false });
console.log(makeItemA('Milk'));
console.log(makeItemB('Milk'));
console.log(makeItemC('Eggs'));

This prints:

undefined
{ name: 'Milk' }
{ name: 'Eggs', done: false }

In makeItemA, name: is read as a label inside a block, so the function runs and returns undefined without any error. With two properties, as in name => { name: name, done: false }, the comma makes the block invalid and the code fails with a SyntaxError before anything runs.

An arrow function has no arguments object, so a rest parameter collects extra arguments instead. An arrow function also cannot be used as a constructor:

const sumAll = (...numbers) => numbers.reduce((total, n) => total + n, 0);
console.log(sumAll(1, 2, 3));
const Point = (x, y) => ({ x, y });
try {
new Point(1, 2);
} catch (error) {
console.log(error.message);
}

This prints:

6
Point is not a constructor

To make objects with new, write a class. To make plain objects without new, a function that returns an object literal, like Point above, is enough.

An arrow function takes this from the code around it rather than from the object it is called on. That suits callbacks inside methods, but an arrow function used as a method does not see its object. Method Shorthand shows the difference, and the THIS keyword page covers this in general.

A line break between the parameters and => is a SyntaxError. Breaking the line after => is allowed, which is the usual way to split a long arrow function:

const describe = (item) =>
`${item.name}: ${item.done ? 'done' : 'to do'}`;
console.log(describe({ name: 'Post the parcel', done: true }));

This prints:

Post the parcel: done

Steve Griffith covers this topic in his video Tips and Gotchas for ES6 Arrow Functions.