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.
MechaPrime ![]()
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.
MechaPrime ![]()
Because {} makes it a block body, your callback returns nothing, so map fills the array with undefined; return the value or drop the braces.
const out = nums.map(n => n * 2)
// or: const out = nums.map(n => { return n * 2 })
Hari ![]()
:: Copyright KIRUPA 2024 //--