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.
Braces need a return
Section titled “Braces need a return”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 8doubleA 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.
No parameters
Section titled “No parameters”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 ready0 1The 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.
Returning an object literal
Section titled “Returning an object literal”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.
No arguments object, and no new
Section titled “No arguments object, and no new”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:
6Point is not a constructorTo 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.
No this of its own
Section titled “No this of its own”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.
Keep the arrow on the parameter line
Section titled “Keep the arrow on the parameter line”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: doneSteve Griffith covers this topic in his video Tips and Gotchas for ES6 Arrow Functions.