Higher‑Order Functions in JavaScript

Hero image for Higher‑Order Functions in JavaScript. Image by Cloe Thornton.
Hero image for 'Higher‑Order Functions in JavaScript.' Image by Cloe Thornton.

The concept of higherorder 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 higherorder 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 higherorder 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 higherorder 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: 15

Here, applyOperation takes a function (operation) as an argument, allowing us to pass different functions to change its behaviour dynamically.


Why Use Higher‑Order Functions?

Higherorder 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 higherorder functions that are commonly used when working with arrays and data transformations.

1. Array.prototype.map()

map() is a higherorder 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: 10

Returning Functions from Functions

Higherorder 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: 15

In 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

Higherorder 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

Higherorder 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

  • Higherorder functions take functions as arguments or return functions.
  • JavaScript provides builtin higherorder functions like map(), filter(), and reduce().
  • Returning functions from functions enables function composition and reusable utilities.
  • Higherorder functions improve modularity, reusability, and readability in JavaScript applications.

Understanding higherorder 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.


Categories:

  1. Development
  2. Front‑End Development
  3. Guides
  4. JavaScript