const arr2 = arr.map(double)
How this is working without me passing the array item? I need to pass a parameter to function double: something like double(item).
let arr = [1, 2, 3, 4, 5]
function double(x) {
return x * 2
}
const arr2 = arr.map(double)
const arr3 = arr.map(item => double(item))
console.log("arr2= ", arr2)
console.log("arr3= ", arr3)
Output:
arr2 = [2, 4, 6, 8, 10]
arr3 = [2, 4, 6, 8, 10]
doubleis a function statement; thus passed directly as callback to an array'smapmethod this function will be invoked with every iteration step ofmap(The implementation of)mapitself takes care of passing the right parameters, the current values ofitem,idx,arrto this callback function.mapis callingdoublein example one. In example two,mapis calling an anonymous arrow function that callsdoublewith a parameter. Both are being passed the current element as its first parameter.itemto the functionitem => double(item)yourself. You’re fine with that but are confused about the functiondouble?itemargument todouble. Since the invocation ofdouble(item)happens as part of the arrow function, the return value ofdouble(item)automatically is the return value of that very arrow function.