Arrow functions and implicit returns
Quick answer
Arrow functions provide a compact function expression. An expression body returns its value implicitly; a block body needs return for a result.
How it works
n => n * 2 has one parameter and an expression body. (a, b) => { return a + b; } uses a block. Arrows do not create their own this binding or arguments object and cannot be used as constructors; receiver details are introduced later.
const double = n => n * 2;const add = (a, b) => { return a + b; };console.log(double(4), add(3, 7));Expected output in Node.js:
8 10Both forms return values, using different body syntax.
const noResult = n => { n * 2; };const result = n => n * 2;console.log(noResult(5), result(5));Expected output in Node.js:
undefined 10A block does not implicitly return its last expression.
Common mistakes
Parentheses are needed for multiple parameters. To implicitly return an object literal, wrap it in parentheses; object literals are taught in Chapter 9.
Practice
Rewrite a function that subtracts two values using both arrow body forms.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Arrow functions and implicit returns — Knowledge Check
Apply the taught concept and reason about the result.
What does the arrow n => n+3 return when called with 4?
What does n => { n+3; } return when called with 4?
Which syntax accepts two named parameters in an arrow?
Which property of arrow functions differs from ordinary methods?
Can an ordinary arrow function be invoked as a constructor using new?