Skip to main content

Command Palette

Search for a command to run...

Understanding Function Composition in JavaScript vs. Mathematics

Updated
3 min readView as Markdown
Understanding Function Composition in JavaScript vs. Mathematics
M

I'm a passionate Backend Engineer with 3 years of experience, specializing in building scalable and efficient systems. I work extensively with Node.js, Express.js, and REST APIs to create robust server-side applications. My database expertise includes MySQL, MongoDB, and Sequelize, while I leverage Redis for caching and optimization. I’m a strong advocate for microservices architecture, designing solutions that are modular and resilient. Through this blog, I share insights, tutorials, and tips from my journey in backend development. Let’s connect and build something amazing together!

Function composition is a fundamental concept in mathematics and functional programming. It allows us to build complex functions by combining simpler ones. But how does function composition in JavaScript relate to its mathematical counterpart? In this article, we’ll explore the similarities and differences, and demonstrate how to implement function composition in JavaScript.


What is Function Composition?

Mathematical Definition

In mathematics, function composition is defined as:

(f∘g)(x)=f(g(x))

This means applying function g to x first, and then applying function f to the result.

For example, given:

  • 𝑔 ( 𝑥 ) = 2 𝑥 g(x)=2x

  • 𝑓 ( 𝑥 ) = 𝑥 + 1 f(x)=x+1

The composition works as follows:

  1. Compute

  2. Then, apply to the result:

  3. So,

JavaScript Equivalent

In JavaScript, function composition follows the same principle. We take multiple functions and execute them in a nested manner.

const g = x => 2 * x;
const f = x => x + 1;

const composedFunction = x => f(g(x));

console.log(composedFunction(3)); // Output: 7

This mirrors the mathematical formula (f ∘ g)(x) = f(g(x)).


Function Composition in JavaScript

While the above example works for two functions, real-world applications often involve composing multiple functions dynamically.

Here’s how we can build a generic compose function in JavaScript:

const compose = (functions) => {
  return (x) => {
    return functions.reduceRight((acc, fn) => fn(acc), x);
  };
};

const increment = x => x + 1;
const double = x => x * 2;
const square = x => x * x;

const composed = compose([increment, double, square]);
console.log(composed(2)); // Output: 9

How It Works:

  1. Square (x²) is applied first: square(2) = 4

  2. Double (2x) is applied next: double(4) = 8

  3. Increment (+1) is applied last: increment(8) = 9

This implementation uses reduceRight, which ensures functions are applied right to left, just like in mathematics.


Differences Between Math and JavaScript Composition

FeatureMathematicsJavaScript Implementation
Syntax( 𝑓 ∘ 𝑔 ) ( 𝑥 ) = 𝑓 ( 𝑔 ( 𝑥 ) ) (f∘g)(x)=f(g(x))compose([f, g])(x)
Order of ExecutionRight-to-left (outer function last)Right-to-left using reduceRight
Function TypesPure mathematical functionsCan be impure (e.g., modify state)
Arity (Inputs)Usually single inputCan accept multiple arguments
ExecutionInstantaneous computationFunction calls execute dynamically

Conclusion

Function composition in JavaScript is inspired by its mathematical counterpart but has some practical differences. JavaScript allows more flexibility, including working with impure functions, multiple arguments, and dynamic function application. Understanding this concept helps developers write cleaner, more modular, and reusable code.

By leveraging function composition, you can break down complex logic into smaller, composable functions, making your code more readable and maintainable.


Image Representation

To visualize function composition, imagine a pipeline where data flows through multiple transformations:

📌 Mathematical Flow:

 x → g(x) → f(g(x))

📌 JavaScript Flow:

 x → [square, double, increment] → final result

(Above is a general representation of function composition from mathematics.)