What is callback part of high order function?

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

this is the callback for that map method - the callback is the function passed as argument of an other function

the function that receive the argument is the higher order function, like map

1 Like

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”.

1 Like

(num)=>{
return num * 2
}
but that isn’t a function num is a variable where as * 2 is part of map, isn’t it?

that’s a function, num is the function parameter, num*2 is the value returned

the map method uses that function, takes each element of the array, feed it to the callback function, and put the result in the new array

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.

all function parameters are “placeholders” as you say

consider

function hello(name) {
console.log("Hello " + name)
}

hello("Emily") // it prints "Hello Emily"

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?

Maybe it helps to rewrite this with more traditional syntax:

const multiple = [1,2,3,4,5];
const result = multiple.map(function(num){
     return num * 2
})

So this is:

const result = multiple.map(callbackFunc)
1 Like

so many good answers. Thank you all for making me understand. Sorry for getting long to understand.

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.