# Compose functions

Build one function that applies functions from right to left, as in f(g(x)).

Canonical: https://1loc.dev/function/compose-functions/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Usage lines executed in Node.js 22 and headless Chromium; no external services
Updated: 2026-10-28

## Implementation

```javascript
function compose(...fns) { return input => fns.reduceRight((value, fn) => fn(value), input); }
```

## Usage

```javascript
const lowercase = text => text.toLowerCase();
const capitalize = text => text.charAt(0).toUpperCase() + text.slice(1);
const reverse = text => [...text].reverse().join('');
compose(reverse, capitalize, lowercase)('HELLO'); // → 'olleH'
```

Lines marked → were executed in Node.js 22 and in headless Chromium, and each result was compared with the value shown. Lines marked e.g. are illustrative.

## Limits

- The last function runs first, as in mathematical notation. pipe lists the same steps in execution order.

The function does not validate its arguments. Add checks where it receives untrusted input.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/compose-functions.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.
