JavaScript

Pipe & Compose

by @admin
10h ago
Apr 28, 2026
Public
pipe applies a list of functions left-to-right (first function runs first), while compose applies them right-to-left. Both pass the result of each step as input to the next. Fundamental building blocks for functional programming in JavaScript — build data transformation pipelines without intermediate variables.
JavaScript
const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x);

const compose = (...fns) => (x) => fns.reduceRight((v, f) => f(v), x);

// Usage
const double    = (x) => x * 2;
const addOne    = (x) => x + 1;
const square    = (x) => x * x;

const transform = pipe(double, addOne, square);
console.log(transform(3)); // ((3*2)+1)^2 = 49

const transform2 = compose(square, addOne, double);
console.log(transform2(3)); // same result: 49
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.