I am new to JS and had been learning with HOF/master it under the hood and I have been little confused, it says it calls a callback. but which part of code is callback? e.g.
const multiple = [1,2,3,4,5];
const result = multiple.map((num)=>{
return num * 2
})
console.log(result)
from my understanding is callback if a function u call inside of a HOF e.g.
function multiplyBy2(array, callback){
let newArr = []
for(let i = 0; i < array.length; i++){
newArr.push(callback(array[i]))
}
return newArr
}
function multiply(num){
return num * 2
}
console.log(multiplyBy2(arr, multiply))
so my question is, what is callback part in higher order function. here the working example of above
A callback is a function you pass to another function. It’s a term from the days of C: nowadays people often just call them “function parameters”, or even just plain “function”.
but num is just a instance of each array value we are passing, isn’t it? like a placeholder until the function is called with a arguement in its placeholder.
so in your example name is a paramater/placeholder but why we call it callback/function paramater in higher order function because from my understanding it is just a placeholder for each array.
I know calling a function inside as a arguement of another function will be called callback? but here i see name in above example and num the same thing?
No worries, we’ve all been there. When learning something new in coding, like HOF, you’ll have to write them at least 50 times in different ways before it really clicks how they work.