Use implicit returns for single-statement arrow functions
masterIf an arrow function body consists of a single statement returning an expression without side effects, omit the braces and use an implicit return. Otherwise, use braces and an explicit return statement.
Note: Do not use implicit returns for functions that have side effects (e.g., modifying a variable outside the function scope).
// good: implicit return
[1, 2, 3].map((number) => `A string containing the ${number + 1}.`);
// good: explicit return for multi-line/complex logic
[1, 2, 3].map((number) => {
const nextNumber = number + 1;
return `A string containing the ${nextNumber}.`;
});
// good: returning an object literal
[1, 2, 3].map((number, index) => ({ [index]: number }));
// No implicit return with side effects
let bool = false;
foo(() => {
bool = true;
});