Search Knowledge

© 2026 LIBREUNI PROJECT

Modern JavaScript & Ecosystem / Functional Foundations

Functional Programming: Higher-Order Functions

The Functional Paradigm

JavaScript is a multi-paradigm language that treats functions as first-class citizens. This means functions can be assigned to variables, passed as arguments to other functions, and returned from functions. This capability is the foundation of Functional Programming (FP).

Higher-Order Functions (HOFs)

A Higher-Order Function is a function that either:

  1. Takes one or more functions as arguments.
  2. Returns a function as its result.

HOFs allow for the abstraction of actions, not just values. This leads to code that is more modular, reusable, and declarative (focusing on “what” to do rather than “how” to do it).

Declarative Array Transformations

The most common application of HOFs in JavaScript is manipulating collections. Instead of using imperative for loops, we use declarative methods on the Array.prototype.

Map: Transforming Elements

The map() method creates a new array by applying a callback function to every element of the calling array. It does not mutate the original array.

const temperaturesCelsius = [0, 10, 25, 30];
const temperaturesFahrenheit = temperaturesCelsius.map(c => (c * 9/5) + 32);

Filter: Selecting Elements

The filter() method creates a new array containing only the elements that satisfy a provided predicate function.

const users = [{ name: 'Alice', active: true }, { name: 'Bob', active: false }];
const activeUsers = users.filter(user => user.active);

Reduce: Consolidating Data

The reduce() method is the most powerful HOF. It executes a “reducer” function on each element, resulting in a single output value (an object, a number, or even another array).

javascript
1const inventory = [
2 { item: 'Laptop', price: 1200 },
3 { item: 'Mouse', price: 25 },
4 { item: 'Monitor', price: 300 }
5];
6 
7const totalValue = inventory.reduce((total, current) => {
8 return total + current.price;
9}, 0); // 0 is the initial value
10 
11console.log("Total Inventory Value:", totalValue);

Currying and Partial Application

Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each take a single argument. This allows for Partial Application, where you fix some arguments of a function and produce a new function for the remaining ones.

javascript
1// Curried function
2const multiply = x => y => x * y;
3 
4const double = multiply(2);
5const triple = multiply(3);
6 
7console.log(double(10)); // 20
8console.log(triple(10)); // 30

Why Functional Programming?

  1. Predictability: Functional code often relies on pure functions, making it easier to reason about and test.
  2. Concurrency: Because FP emphasizes immutability, it avoids race conditions associated with shared mutable state.
  3. Brevity: Declarative code is typically more concise than imperative equivalents.

Which array method should be used to create a subset of an array based on a condition, without modifying the original array?