I need help understanding the higher-order function and callback functions

Hello fellow coders!

I need some understanding to what higher-oder function does to the code when declared. I understand that higher-order functions simply change the passing function as a parameter and returns it as a function. I get confused a lot by invoking a higher-order function to the code when I don’t know where to put them. In the example Codecademy gave,

const timeFuncRuntime = funcParameter => {
let t1 = Date.now();
funcParameter();
let t2 = Date.now();
return t2 - t1;
}

const addOneToOne = () => 1 + 1;

timeFuncRuntime(addOneToOne);

  1. is the funcParameter considered the higher-order function and the timeFuncRuntime is the normal function declared by the const?
  2. and the callback function doesn’t seem to make sense to me at first then I looked at the code more closely, but to call back the code we need a parameter (correct me if im wrong) and I saw AddOneToOne mentioned. is that considered a piece of the callback function that I missed?

timeFuncRuntime(() => {
for (let i = 10; i>0; i–){
console.log(i);
}
});

also, is this different from the previous lessons like in the arrays where the code mutates when we add an additional function in the code, like .splice, .shift etc?
I just need someone to explain this to me nicely. Thanks a bunch!

A callback is a reference to another function. Functions are values so can be used as arguments in a function call. We don’t pass the return value of the function, but a reference to the function itself. The function we pass the reference to will invoke it as needs be.

Notice above how funcParameter is a variable. However, that variable is a reference (in this instance) to the addOneToOne function. As such, it can be invoked under the local name.

Higher order functions are abstractions. They let us break our code up into smaller pieces that do only one thing, or very few things. timeFuncRuntime() is the higher order function in this case since it is the one that operates on another function. The parameter is an action, rather than being a value.

This is a topic that bears further reading. Search out the term and see what articles and tutorials come to the surface. There is no real magic here, just abstraction.

1 Like

Hey denzelsugayan6884482

Nope, is exactly the other way around. You call:
timeFuncRuntime(addOneToOne);
that calls:
Date.now();
and binds its result to:
let t1
then calls:
funcParameter();
and so on.

is that considered a piece of the callback function that I missed?

Yes. But not a piece. Is an actual function that has been defined and that is called (back) inside another function, this other function will be its higher order f.

also, is this different from the previous lessons like in the arrays where the code mutates when we add an additional function in the code, like .splice, .shift etc?

Unfortunately I don’t know what lessons you’re referring to. But if you post a code snippet and highlight what confuses you, I’ll be glad to help if I can. :slight_smile: