Sub topic 15
Implicit Returns in Arrow Functions
This chapter assumes you already know how return works in regular functions, including the implicit undefined a function produces when it has no return. It also assumes you can write basic arrow functions such as (x) => ....
Two Kinds of Arrow Function Body
An arrow function can have one of two kinds of body.
- An expression body is a single expression after the
=>. JavaScript evaluates it and returns the result automatically. This is called an implicit return. - A block body is wrapped in curly braces
{ }. It works like a regular function body. You need an explicitreturn, or the function returnsundefined.
const doubleExpr = (x) => x * 2;
const doubleBlock = (x) => {
return x * 2;
};
const doubleForgot = (x) => {
x * 2; // computed, then thrown away
};
console.log(doubleExpr(4)); // 8
console.log(doubleBlock(4)); // 8
console.log(doubleForgot(4)); // undefinedThe parser makes the choice for you. If the first character after => is {, the body is a block.
Returning Object Literals
Because of that rule, returning an object literal causes trouble. The braces of the object are read as a block.
const toPoint = (x, y) => ({ x, y });
const broken = (x, y) => { x, y };
console.log(toPoint(1, 2)); // { x: 1, y: 2 }
console.log(broken(1, 2)); // undefinedIn broken, the body is a block that contains the comma expression x, y. It runs and is discarded, so the function returns undefined. No error is raised. Wrapping the object in parentheses forces the parser to read it as an expression.
Longer Expressions
An expression body can span several lines if you wrap it in parentheses. A ternary works when the result depends on a condition.
const label = (n) => (n === 1 ? "1 item" : `${n} items`);
const summary = (user) => (
`${user.name} (${user.role})`
);
console.log(label(1)); // "1 item"
console.log(label(3)); // "3 items"
console.log(summary({ name: "Ana", role: "admin" })); // "Ana (admin)"Implicit returns are a good fit for short callbacks passed to methods like map and filter:
const users = [{ name: "Ana", active: true }, { name: "Bo", active: false }];
const names = users.filter((u) => u.active).map((u) => u.name);
console.log(names); // ["Ana"]If you need statements such as if, const, or a loop, switch to a block body and write return explicitly.
Common Errors
| Mistake | Result | Fix |
|---|---|---|
| `() => { value }` | `undefined` | `() => value` |
| `() => { a: 1 }` | `undefined` | `() => ({ a: 1 })` |
| Adding braces later without adding `return` | `undefined` | Add `return` |
Exercise
What does const f = (a) => { a + 1 }; return for f(2), and what are two ways to make it return 3?
Show answer
It returns undefined, because the braces make a block body with no return statement. To fix it, either remove the braces, (a) => a + 1, or add an explicit return, (a) => { return a + 1; }.
Exercise
Write an arrow function makeUser that takes name and returns an object { name, active: true } using an implicit return.
Show answer
const makeUser = (name) => ({ name, active: true }); The parentheses make the braces an object literal instead of a block.