Skip to content

higher-order function

A higher-order function is a function that takes another function as an argument, returns a function as its result, or does both.

JavaScript functions are objects that can be called, so code can store them in variables, pass them to other functions, and return them like any other value. A function passed this way is a callback, and the function that receives it is the higher-order function. A returned function often forms a closure over the arguments of the call that created it.

Higher-order functions show up throughout everyday JavaScript. Array methods such as .map(), .filter(), and .toSorted() take a function that decides what happens to each element. The .bind() method of Function returns a new function with a fixed this binding.

Wrappers that add logging, caching, or retry logic to existing code are higher-order functions too. They take a function and return a new one that callers use the same way.

Example

A checkout page should charge a card only once, even when a double click runs the payment handler twice. A higher-order once() function takes the payment function and returns a guarded version of it:

Language: JavaScript
> function once(action) {
…   let called = false;
…   let result;
…   return (...args) => {
…     if (!called) {
…       called = true;
…       result = action(...args);
…     }
…     return result;
…   };
… }
undefined
> let charges = 0;
undefined
> const chargeCard = once((amount) => {
…   charges += 1;
…   return `Charged $${amount}`;
… });
undefined

> chargeCard(40)
'Charged $40'
> chargeCard(40)
'Charged $40'
> charges
1

The returned arrow function closes over called and result, so every call to chargeCard() shares them. The first call runs the original function. Later calls return the saved result without running it again, which the charges counter confirms.

The widget below steps through repeated chargeCard() calls and shows which lines run and how the called and result bindings change:

Interactive diagram — enable JavaScript to view.

Returning a function also lets code build callbacks from settings. A byField() helper creates a comparator, the function that .toSorted() calls to decide which of two elements comes first:

Language: JavaScript
> const byField = (field) => (a, b) => a[field] - b[field];
undefined
> const products = [
…   { name: "Mug", price: 12 },
…   { name: "Lamp", price: 30 },
…   { name: "Pen", price: 3 },
… ];
undefined

> products.toSorted(byField("price")).map((product) => product.name)
[ 'Pen', 'Mug', 'Lamp' ]

Each call to byField() returns a new comparator that remembers its field argument. The chain passes a function to two higher-order methods. The .toSorted() method gets the comparator, and .map() gets the arrow function that picks each name.

A developer pours shapes into a hopper feeding nested domes that hold a counter labeled CLOSURE, while capsules roll out under a bell and a clock.

Course

JavaScript Scope, Closures, and Functional Patterns for Pythonistas

Port a bouldering gym's front-desk script to JavaScript, and learn where names live and when they exist, how closures keep state, and how to work with rest parameters, callbacks, and timers.

intermediate js py

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 15, 2026