Higher-Order Functions in JavaScript
7 min read·Jan 1, 2025
As opposed to a first-order function, a higher-order function is a function that takes other functions as argument:
function higherOrder(firstClass) {
// ...
}
Or returns a function as its result:
function higherOrder() {
return function() {
// ...
}
}
When supplied as an argument or returned as a value, these functions are called first-class functions.
Note: First-class functions that are typically executed in response to an event but not returned are usually called callback functions.
function higherOrder(callback) { callback(); }
Example
Let's consider this script, that uses a higher-order function to perform simple arithmetic calculation:
function calculate(a, b, fn) {
return fn(a, b);
}
const add = (a, b) => a + b;
const multiply = (a, b) => a * b;
const sum = calculate(3, 2, add);
console.log(sum);
const product = calculate(3, 2, multiply);
console.log(product);
When executed, it will:
- Define a function named
calculatethat returns the execution result of its callback functionfnand its two parametersaandb. - Declare a variable named
addand initialize it with an anonymous function that returns the sum of its parametersaandb. - Declare a variable named
multiplyand initialize it with an anonymous function that returns the product of its parametersaandb. - Execute the
calculatefunction with two integers and theaddfunction and store its result into thesumvariable. - Output the value of the
sumvariable. - Execute the
calculatefunction with two integers and themultiplyfunction and store its result into theproductvariable. - Output the value of the
productvariable.
Which will produce this output:
$ node calculate.js
5
6
Note: Since the functions stored in the
addandmultiplyvariables are anonymous, they can also be directly declared as an argument of thecalculatefunction during its invocation:const sum = calculate(1, 2, (a, b) => a + b); const product = calculate(1, 2, (a, b) => a * b);
Closures
A closure is a function that has access to its own scope, the scope of the outer function, and the global scope.
It keeps track of the variables that are in scope at the time of its creation, and can still access them even after the outer function has finished executing.
Example
Let's consider this script, that implements a closure whose return function still has access to a local scope variable after its creation:
function createCounter() {
let count = 0;
return function () {
count = count + 1;
return count;
};
}
const counter = createCounter();
console.log(counter());
console.log(counter());
console.log(counter());
When executed, it will:
-
Define a function named
createCounter.- Declare a local variable named
countand initialize it to0. - Return an anonymous function that increments the value of the
countvariable by1and returns it.
- Declare a local variable named
-
Execute the
createCounterfunction and store its result into thecountervariable. -
Execute the
countervariable as a function 3 times and output its return value.
Which will produce this output:
$ node create_counter.js
1
2
3
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery