What Makes a Function Higher-Order
You'll learn the technical definition of a higher-order function and see exactly what qualifies a function for this category.
Now that you understand functions are first-class citizens in JavaScript, you can leverage them in powerful ways. A higher-order function is simply a function that does at least one of these two things:
- Accepts one or more functions as arguments, OR
- Returns a function as its result
That's it! If a function does either (or both), it's higher-order.
Why This Matters
Higher-order functions let you write more abstract, reusable code. Instead of hard-coding specific behavior, you accept behavior as a parameter. Instead of repeating similar logic, you create functions that build other functions.
Examples of Each Type
Accepting functions as arguments:
function repeat(times, action) {
for (let i = 0; i < times; i++) {
action(i);
}
}
repeat(3, console.log); // Logs: 0, 1, 2
repeat is higher-order because it accepts action (a function) as a parameter.
Returning a function:
function createMultiplier(factor) {
return function(number) {
return number * factor;
};
}
const double = createMultiplier(2);
console.log(double(5)); // 10
createMultiplier is higher-order because it returns a new function.
Think of higher-order functions as function factories or behavior orchestrators—they work with functions the way regular functions work with numbers or strings.
Key Takeaway: A function is higher-order if it accepts functions as arguments, returns functions, or does both—this enables powerful abstraction and code reuse.