
Higher‑Order Functions in JavaScript

The concept of higher‑order functions in JavaScript describes a function construct that allows a function to pass another function as an argument, return functions from other functions, and compose more expressive logic. How does that sound? Complicated right? It's not that bad. Basically, it enables us to write more concise, modular, and reusable code.
In this article, I'll do my best to explain it all to you, explore what higher‑order functions are, why they are useful, and how they are commonly used in JavaScript.
What are Higher‑Order Functions?
In JavaScript, a function is considered a higher‑order function if it does one or both of the following:
- Accepts another function as an argument.
- Returns a function as its result.
Here's a simple example of a higher‑order function:
const applyOperation = (operation: (a: number, b: number) => number, x: number, y: number): number => { return operation(x, y);};const add = (a: number, b: number) => a + b;const multiply = (a: number, b: number) => a * b;console.log(applyOperation(add, 5, 3)); // Output: 8console.log(applyOperation(multiply, 5, 3)); // Output: 15Here, applyOperation takes a function (operation) as an argument, allowing us to pass different functions to change its behaviour dynamically.
Why Use Higher‑Order Functions?
Higher‑order functions help us write cleaner and more maintainable code by promoting:
Code reusability
– Common logic can be abstracted into reusable functions.Modularity
– Functions become more composable and easier to work with.Functional programming principles
– JavaScript's functional nature allows us to build more declarative and expressive code.
I'm very aware that this is a lot of words where a few examples might be easier to digest, so...
Built‑In Higher‑Order Functions in JavaScript
JavaScript provides several higher‑order functions that are commonly used when working with arrays and data transformations.
1. Array.prototype.map()
map() is a higher‑order function that creates a new array by applying a function to each element.
For example:
const numbers = [1, 2, 3, 4];const doubled = numbers.map(num => num * 2);console.log(doubled); // Output: [2, 4, 6, 8]2. Array.prototype.filter()
filter() creates a new array with elements that satisfy a given condition:
const numbers = [1, 2, 3, 4, 5];const evenNumbers = numbers.filter(num => num % 2 === 0);console.log(evenNumbers); // Output: [2, 4]3. Array.prototype.reduce()
reduce() executes a reducer function on each element of an array, returning a single accumulated value, like this:
const numbers = [1, 2, 3, 4];const sum = numbers.reduce((acc, num) => acc + num, 0);console.log(sum); // Output: 10Returning Functions from Functions
Higher‑order functions can also return functions, allowing for greater flexibility and composition. For example:
const createMultiplier = (factor: number) => (num: number) => num * factor;const double = createMultiplier(2);const triple = createMultiplier(3);console.log(double(5)); // Output: 10console.log(triple(5)); // Output: 15In this example, createMultiplier returns a new function that multiplies a given number by the provided factor. This approach is useful for function factories and custom utilities.
Practical Uses of Higher‑Order Functions
1. Function Composition
Function composition is the process of combining multiple functions into a single function, like this:
const greet = (name: string) => `Hello, ${name}!`;const excited = (str: string) => str.toUpperCase();const greetExcited = (name: string) => excited(greet(name));console.log(greetExcited("Maddie")); // Output: HELLO, MADDIE!2. Event Listeners
Higher‑order functions are commonly used in event handling, too.
const handleClick = (callback: () => void) => { document.addEventListener("click", callback);};handleClick(() => console.log("Button clicked!"));In this way, we can abstract away event handling, making it reusable across different elements.
Wrapping up
Higher‑order functions are a powerful feature in JavaScript which enables cleaner, more modular, and reusable code. They allow us to pass and return functions, promoting functional programming principles and improving code maintainability.
Key Takeaways
- Higher‑order functions take functions as arguments or return functions.
- JavaScript provides built‑in higher‑order functions like
map(),filter(), andreduce(). - Returning functions from functions enables function composition and reusable utilities.
- Higher‑order functions improve modularity, reusability, and readability in JavaScript applications.
Understanding higher‑order functions allows us to write more modular and reusable JavaScript code, making it easier to maintain (for you, the rest of your team, and whoever comes to your code next) and extend your applications.
Related Articles

Improve Page Performance with content‑visibility. Improve Page Performance with

What are Higher‑Order Components in React? What are Higher‑Order Components in React?

What is a Static Site Generator? What is a Static Site Generator?

Valid Palindrome in JavaScript: Two Pointers and Normalisation. Valid Palindrome in JavaScript: Two Pointers and Normalisation

Object.keys(), Object.values(), and Object.entries() Explained. Object.keys(),Object.values(), andObject.entries()Explained
Harnessing the Power of Prototype.bind(). Harnessing the Power of
Prototype.bind()
Cleaning up Your JavaScript Code: The Double Bang (!!) Operator. Cleaning up Your JavaScript Code: The Double Bang (
!!) Operator
What A Levels Do You Need for Software Engineering? What A Levels Do You Need for Software Engineering?

Finding the Median of Two Sorted Arrays with JavaScript. Finding the Median of Two Sorted Arrays with JavaScript

Understanding the Nullish Coalescing (??) Operator in JavaScript. Understanding the Nullish Coalescing (
??) Operator in JavaScript
Creating Progressive Web Apps (PWAs) with Angular. Creating Progressive Web Apps (PWAs) with Angular

Replace Inline Styles in Gatsby with an External CSS File. Replace Inline Styles in Gatsby with an External CSS File