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
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
Yep, with {} the arrow function has a block body, so map gets undefined unless you return explicitly.
Sora
Exactly-arr.map(x => ({ value: x })) returns an object, but arr.map(x => { value: x }) is just a block with no return.
MechaPrime
:: Copyright KIRUPA 2024 //--