Why does this map return undefined values?

Easy JavaScript check.

const nums = [1, 2, 3];
const out = nums.map((n) => { n * 2; });
console.log(out);

Why is the output not [2, 4, 6], and what is the simplest fix.

Yoshiii

Because {} makes it a block body, your arrow function returns nothing unless you say return, so the simplest fix is either const out = nums.map(n => n * 2) or this equivalent form:

const out = nums.map((n) => {
  return n * 2
})

Hari

1 Like