Sub topic 6
Arrow Function Syntax and Implicit Returns
This chapter assumes you already know how to write function expressions and assign them to variables with const. Arrow functions are a shorter way to write a function expression. This chapter covers only their syntax: how to write parameters, and when the function returns a value without the return keyword. Later chapters cover how arrows handle this, arguments, and new.
The basic shape
An arrow function has a parameter list, the => token, and a body:
const multiply = function (a, b) {
return a * b;
};
const multiplyArrow = (a, b) => a * b;
console.log(multiply(2, 3)); // 6
console.log(multiplyArrow(2, 3)); // 6Both versions behave the same here. The arrow version drops the function keyword, the braces, and return.
Parameter rules
The parentheses around the parameters are optional in only one case: exactly one plain parameter.
| Parameters | Syntax | Parentheses required? |
|---|---|---|
| None | `() => 42` | Yes |
| One plain name | `x => x * 2` | No |
| Two or more | `(a, b) => a + b` | Yes |
| Default value | `(x = 1) => x` | Yes |
| Rest parameter | `(...xs) => xs.length` | Yes |
| Destructuring | `({ id }) => id` | Yes |
Many teams always use parentheses so the code looks the same everywhere. Formatters such as Prettier do this by default.
Concise bodies and block bodies
The body after => decides whether the function returns a value automatically.
- A concise body is a single expression. Its value is returned implicitly.
- A block body starts with
{. It works like a normal function body, so you need an explicitreturn. Without one, the function returnsundefined.
The object literal trap
The parser always reads a { right after => as the start of a block body. That means you cannot return an object literal by writing it directly. Wrap the object in parentheses so the parser reads it as an expression:
const square = x => x * x;
const sum = (a, b) => a + b;
const makePoint = (x, y) => ({ x, y });
const noReturn = () => {
x: 1;
};
console.log(square(4)); // 16
console.log(sum(1, 2)); // 3
console.log(makePoint(1, 2)); // { x: 1, y: 2 }
console.log(noReturn()); // undefinednoReturn does not throw an error. JavaScript reads x: as a statement label and 1; as an expression statement that does nothing. The function finishes without a return, so it returns undefined. Because no error is raised, the bug can go unnoticed for a long time.
Line breaks and multi-line expressions
A concise body can span several lines. Wrap it in parentheses to keep it readable:
const describe = (user) => (
user.active
? `${user.name} is active`
: `${user.name} is inactive`
);
console.log(describe({ name: 'Ada', active: true })); // Ada is activeYou cannot put a line break between the parameter list and =>. Writing (a, b) on one line and => a + b on the next is a SyntaxError.
Common errors
- Adding braces to a one-line arrow and forgetting
return. This often happens insidemap:items.map(x => { x * 2 })produces an array ofundefined. - Returning an object literal without parentheses.
- Leaving out parentheses when you add a default value or a second parameter later.
Exercise
What does [1, 2, 3].map(n => { value: n }) return, and how do you fix it so it returns an array of objects?
Show answer
It returns [undefined, undefined, undefined]. The braces form a block body, value: is a label, and the function has no return statement. Write [1, 2, 3].map(n => ({ value: n })) to get [{ value: 1 }, { value: 2 }, { value: 3 }].
Exercise
Rewrite const area = function (w, h = w) { return w * h; }; as an arrow function with a concise body.
Show answer
const area = (w, h = w) => w * h; The parentheses are required because there are two parameters and one has a default value.