Skip to main content

Higher-Order Function

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/higher-order-function

In short

A higher-order function is a function that takes another function as an argument, returns a function, or both, so behavior can be passed around like data.

What is a higher-order function?

A higher-order function is any function that works with other functions as values: it accepts a function as a parameter, returns a new function, or does both. This is possible in languages where functions are first-class values, which means they can be stored in variables, passed as arguments and returned just like numbers or strings. JavaScript, Python, Kotlin, Swift, Rust and almost every other modern language support this.

The best-known examples are array helpers such as map, filter and reduce. Each one handles the looping for you and asks only for a small function describing what to do with each item: how to transform it, whether to keep it, or how to combine it. Functions that return functions are also common, such as a withLogging(fn) wrapper that returns a version of fn that logs every call, or a factory that builds customized validators.

A higher-order function is like a food processor with swappable blades: the machine provides the motor and the bowl, and the blade you attach decides whether it slices, grates or blends. This makes code shorter and more reusable, because the general mechanics are written once and only the part that varies is passed in.

Higher-order functions are often mixed up with callbacks. A callback is the function that gets passed in, while the higher-order function is the one that receives it, so in items.map(double) the map method is higher-order and double is the callback. Functions returned from a higher-order function are usually closures, because they remember variables from the function that created them.

Key takeaways

  • A higher-order function takes a function as input, returns a function, or both.
  • It requires first-class functions, which most modern languages support.
  • map, filter and reduce are the classic examples.
  • The function passed in is the callback; the function receiving it is higher-order.

Example

Taking and returning functionsjavascript
const prices = [12, 45, 7, 30];

// filter and map are higher-order: they take functions as arguments
const doubled = prices
  .filter((p) => p > 10)
  .map((p) => p * 2);

// A higher-order function that returns a new function
function multiplier(factor) {
  return (n) => n * factor;
}
const triple = multiplier(3);

console.log(doubled, triple(5)); // [24, 90, 60] 15

Readers ask

Is a callback a higher-order function?

Not usually. The callback is the function being passed in, and the function that accepts it is the higher-order one. A callback is only higher-order itself if it also takes or returns functions.

Which languages support higher-order functions?

Any language with first-class functions, including JavaScript, TypeScript, Python, Kotlin, Swift, C#, Rust, Go and all functional languages. Java added support through lambdas and functional interfaces in Java 8.

Why use higher-order functions instead of loops?

They hide repetitive looping code and let you describe what should happen to each item, which is often shorter and easier to read. Plain loops are still fine, and sometimes clearer when the logic has many steps or needs to stop early.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings