Arrow Functions
An arrow function is a function expression with concise syntax and lexical this. An expression body returns its value; a block body needs an explicit return.
Changing the function’s braces can change whether a result is returned.
See the mechanism
36Follow one example
double has an expression body and implicitly returns 6. broken has a block body: its calculation runs but no return supplies it to the caller, so the result is undefined. To return an object literal concisely, wrap it in parentheses. Arrow functions also capture this from their surrounding scope; do not replace every method with an arrow.
Predict, change and check
Predict: What single keyword would make the block-bodied version return 6?
Test your explanation: Add return inside broken. Then replace double with a block-bodied version and preserve the same result.
The tasks below ask you to explain that result and apply the idea to different inputs.
Apply and distinguish
Choose an arrow for the right binding behavior
An arrow function has lexical this and arguments rather than creating ordinary call-specific bindings for them. It is not a constructor and cannot be invoked with new. That makes it useful for callbacks but not a drop-in replacement for every method definition.
An expression body returns its expression; a block body needs an explicit return. Returning an object literal from an expression body needs parentheses so the braces are not parsed as a statement block. Inspect a callback that unexpectedly returns undefined before blaming the array method that calls it.
Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.