Could you please have a look at the following example code, and explain, why when we pass previously declered function to Array method, we don’t need to invoke this function on every element, but just pass it? How the compiler know to invoke callback function on every element?
const numbers = [2, 7, 9, 171, 52, 33, 14];
const toSquare = num => num * num;
// both functions give the same result
const squareNums1 = arr => arr.map(toSquare);
const squareNums2 = arr => arr.map(el => toSquare(el));
If we invoke the callback function when passing it to a method, we’ll be passing the value of the callback function to the method, not the function itself.