Skip to main content

Higher-Order Functions ⚡

Higher-order functions are functions that take other functions as arguments or return functions as their result.

Higher-order functions leverage closures to "remember" information even after the outer function has finished executing.

Example:

function higherOrderFunction(callback) {
return callback();
}

function sayHello() {
return "Hello!";
}

higherOrderFunction(sayHello); // returns "Hello!"

Difference Between Callback Function and Higher-Order Function (HOF)

No, a callback function is not a higher-order function (HOF), but it is used inside a higher-order function.

FeatureCallback FunctionHigher-Order Function (HOF)
DefinitionA function passed as an argument to another functionA function that takes a function as an argument or returns a function
PurposeTo be executed later, usually after an event or operationTo manipulate functions by taking or returning them
ExamplesetTimeout(() => console.log("Hello"), 1000);arr.map(num => num * 2);
Who Uses It?Used inside a higher-order functionUses callbacks to process logic