Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
👋 Hi! I'm Dastan, author of Hack Frontend. Always open to professional networking => connect with me on LinkedIn.
Higher-Order Function (HOF) is a function that takes another function as an argument or returns a function as a result.
Simply Put:
If a function can work with another function (accept, return or both), it's a higher-order function.
function greet(name) {
return `Hello, ${name}`;
}
function processUserInput(callback) {
const name = "Alice";
return callback(name);
}
console.log(processUserInput(greet)); // "Hello, Alice"
Here processUserInput is a higher-order function since it takes another function greet as an argument.
function multiplier(factor) {
return function (number) {
return number * factor;
};
}
const double = multiplier(2);
console.log(double(5)); // 10
In this example multiplier returns another function — so it's also a higher-order function.
JavaScript actively uses HOF in built-in array methods: map, filter, reduce, forEach.
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]
The map method is a higher-order function because it accepts another function (num => num * 2) as an argument.
Higher-order functions are functions that: