Underscore.js _.compose() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Function

Definition

Returns the composition of a list of functions, where each function consumes the return value of the function that follows. In math terms, composing f, g, and h produces f(g(h())) (official Underscore docs). Stable Function helper — not deprecated.

Pass the required arguments shown below. Underscore function helpers usually return a new function — check timing and call-count behavior carefully.

Syntax

Call _.compose. Start with this line.

Underscore
_.compose(*functions)
CallWhat it does
_.compose(*functions)Primary signature.
_.compose(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.compose(*functions)

Parameters

ParameterDescription
*functionsRequired. Functions to compose (rightmost runs first)

Return Value

Return ValueDescription
*New composed function

Underscore.js Examples

Example 1 — Official: greet ∘ exclaim

Uppercase first, then greet.

Underscore
var greet = function (name) {
  return "hi: " + name;
};
var exclaim = function (statement) {
  return statement.toUpperCase() + "!";
};

var welcome = _.compose(greet, exclaim);
welcome("moe");
// → 'hi: MOE!'
Try It Yourself

How It Works

  1. exclaim is listed last → runs first on 'moe'.
  2. 'MOE!' is passed into greet.
  3. greet prepends 'hi: '.
  4. Same as greet(exclaim('moe')).

Example 2 — Number Pipeline

Compose three simple transforms.

Underscore
var add1 = function (n) { return n + 1; };
var double = function (n) { return n * 2; };
var square = function (n) { return n * n; };

var pipeline = _.compose(square, double, add1);
pipeline(3);
// add1(3) → 4
// double(4) → 8
// square(8) → 64
// → 64
Try It Yourself

How It Works

  1. Rightmost add1 receives 3.
  2. Then double, then square.
  3. Leftmost produces the final value.
  4. Swap order in _.compose(...) to change the math.

Example 3 — Order Matters

Same functions, reversed list → different result.

Underscore
var greet = function (name) {
  return "hi: " + name;
};
var exclaim = function (s) {
  return s.toUpperCase() + "!";
};

_.compose(greet, exclaim)("moe");
// → 'hi: MOE!'

_.compose(exclaim, greet)("moe");
// → 'HI: MOE!'
Try It Yourself

How It Works

  1. First compose: uppercase, then greet.
  2. Second: greet first → 'hi: moe', then uppercase whole string.
  3. List order is the composition order.
  4. For left-to-right reading, reverse the functions you pass.

Example 4 — Single Function

Composing one function is a thin wrapper.

Underscore
var double = function (n) { return n * 2; };
var only = _.compose(double);

only(5);
// → 10

// Still useful as a uniform “pipeline” API
// when sometimes you pass one step, sometimes many.
Try It Yourself

How It Works

  1. One function → that function gets all call args.
  2. Result matches calling double(5) directly.
  3. Handy when building pipelines dynamically.
  4. Need decoration? Prefer _.wrap.

Browser & Runtime Support

_.compose ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.compose()

Available in [email protected] (UMD / ESM). Right-to-left composition. Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.compose Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.compose(*functions) returns a new function that is the composition of those functions. Official: _.compose(greet, exclaim)('moe') → 'hi: MOE!' which is greet(exclaim('moe')).
Right to left. The last function you pass gets the original arguments; each result flows leftward. Math style: f(g(h(x))).
Only the rightmost function. Every other function receives exactly one value — the previous return value.
_.compose is a fixed pipeline of outputs. _.wrap gives your wrapper the original function so it can call it conditionally and add before/after logic.
Underscore’s _.compose is right-to-left. For reading order left-to-right, reverse the function list (or use a custom pipe helper).
Yes. _.compose(fn) returns a function that simply calls fn with the given arguments.

Next: _.restArguments()

Turn trailing arguments into a single array — like rest parameters.

_.restArguments() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful