Implicit return and the object-literal trap
In makeUserWrong, { name: name } is read as a block containing a label name:, so nothing is returned. Parentheses make it an object.
JavaScript has three main ways to write a function: a declaration (function add() {}), a function expression stored in a variable, and a short arrow function ((a, b) => a + b); they differ in hoisting, this, arguments and whether they can be used with new.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Rewrite function isAdult(age) { return age >= 18; } as an arrow function with a concise body.
Write an arrow toPoint that takes x and y and returns an object { x, y } with an implicit return.
Use an arrow with map to turn [1, 2, 3] into ["#1", "#2", "#3"].
Explain it without notes
What is the difference between a function declaration and a function expression?
What is an implicit return, and how do you implicitly return an object from an arrow function?
Name the behavioural differences between arrow functions and regular functions.
When would you choose an arrow function, and when a regular one?
Console
(nothing was printed)Expected console output
8
undefined
undefined
{ name: 'Asha' }
[ 10, 20, 30 ]