Back to questions
JuniorTheoryCommon
What does the program print?
const twice = (x) => {
x * 2;
};
console.log(twice(4));undefined
- Expecting
8, as from(x) => x * 2: once the body is in braces, onlyreturngives a value back, and without it the call returnsundefined. - Expecting
nullfrom a function that returns nothing: a call withoutreturnalways givesundefined;nullappears only when it is set on purpose.
- How would you fix
twiceso that it prints8, in two different ways? - Why does an arrow function that returns an object literal need
({ … })?