Why does my JavaScript map() return undefined instead of the expected values? [duplicate]

22 hours ago 4
ARTICLE AD BOX

This is because you used a wrong syntax for the arrow function passed to numbers.map.

This is the fix:

const numbers = [1, 2, 3, 4]; const doubled = numbers.map(num => num * 2); console.log(doubled);

Alternative Fix:

Alternatively, you could have used the block syntax for your arrow function, but then it should be fixed just by adding the keyword return:

const numbers = [1, 2, 3, 4]; const doubled = numbers.map(num => { return num * 2; }); console.log(doubled);

In this particular case, the block {...} can be considered redundant. The shorter first variant would suffice.

Background:

When you use the arrow function syntax with {...}, you have to use return statement inside the curly brackets, but it was missing. And a function with missing return actually returns undefined.

Read Entire Article