Underscore.js _.chain() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Chaining

Definition

Returns a wrapped object. Calling methods on this object continues to return wrapped objects until value is called (official Underscore docs). Stable chaining helper — not deprecated.

Pass the required arguments shown below. Chained steps keep returning wrappers until you call .value() for the plain result.

Syntax

Call _.chain. Start with this line.

Underscore
_.chain(obj) or _(obj).chain()
CallWhat it does
_.chain(obj) or _(obj).chain()Primary signature.
_.chain(...)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
_.chain(obj) or _(obj).chain()

Parameters

ParameterDescription
objRequired. Value to wrap
Chained methodsOptional. Each returns another wrapper
.value()Required. Unwraps the final plain value

Return Value

Return ValueDescription
*The value returned by _.chain().

Underscore.js Examples

Example 1 — Official Docs Stooges

Sort by age, format a sentence, take the first — unwrap with .value().

Underscore
var stooges = [
  { name: "curly", age: 25 },
  { name: "moe", age: 21 },
  { name: "larry", age: 23 }
];

_.chain(stooges)
  .sortBy(function (s) { return s.age; })
  .map(function (s) { return s.name + " is " + s.age; })
  .first()
  .value();
// → "moe is 21"
Try It Yourself

How It Works

  1. Wrap. _.chain(stooges) starts the fluent wrapper.
  2. Sort. Ages become 21 → 23 → 25, so moe is first.
  3. Map. Each stooge becomes a sentence like "moe is 21".
  4. First + value. Keep the first sentence, then unwrap the string.

Example 2 — A Wrapper Until .value()

Skip .value() and you still have a wrapper, not an array.

Underscore
var wrapped = _.chain([1, 2, 3]).map(function (n) {
  return n * 2;
});

typeof wrapped.value;     // "function"
Array.isArray(wrapped);   // false
wrapped.value();          // [2, 4, 6]
Try It Yourself

How It Works

  1. Still wrapped. After map, Underscore returns another chain wrapper.
  2. Not an array. Array.isArray(wrapped) is false.
  3. Unwrap. .value() extracts the plain [2, 4, 6].
  4. Rule. Treat missing .value() as unfinished work.

Example 3 — Official Lyrics Word Count

Map lines to words, flatten, then reduce into counts.

Underscore
var lyrics = [
  { line: 1, words: "I'm a lumberjack and I'm okay" },
  { line: 2, words: "I sleep all night and I work all day" },
  { line: 3, words: "He's a lumberjack and he's okay" },
  { line: 4, words: "He sleeps all night and he works all day" }
];

var counts = _.chain(lyrics)
  .map(function (line) { return line.words.split(" "); })
  .flatten()
  .reduce(function (memo, word) {
    memo[word] = (memo[word] || 0) + 1;
    return memo;
  }, {})
  .value();

counts.lumberjack;  // 2
counts.all;         // 4
counts.night;       // 2
Try It Yourself

How It Works

  1. Map. Each line becomes an array of words.
  2. Flatten. One big word list from all lines.
  3. Reduce. Count how often each word appears.
  4. Docs highlight. lumberjack → 2, all → 4, night → 2.

Example 4 — Proxied reverse Mutates

Array prototype methods on a chain can change the original array.

Underscore
var nums = [1, 2, 3];

_.chain(nums).reverse().value();
// → [3, 2, 1]

nums;
// → [3, 2, 1]  (mutated!)
Try It Yourself

How It Works

  1. Proxy. The wrapper calls native Array#reverse.
  2. Side effect. That reverses nums in place.
  3. Safer pattern. _.chain(nums.slice()).reverse().value() keeps the original.
  4. Prefer Underscore helpers like map/filter when you want new collections only.

Browser & Runtime Support

_.chain 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

_.chain()

Available in [email protected] (UMD / ESM). 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
_.chain 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

_.chain(obj) returns a wrapped object. Underscore methods on that wrapper keep returning wrappers until you call .value() to get the final plain result.
_.chain(stooges).sortBy(age).map(name + age).first().value() returns "moe is 21".
Each chained step returns another wrapper, not a plain array or string. .value() extracts the result. Skipping it leaves a wrapper object.
No. _(obj).map(...) usually returns a plain array after one method. _.chain(obj) (or _(obj).chain()) keeps every step wrapped until .value().
_.chain itself only wraps. Helpers like map and sortBy return new collections. Proxied Array methods such as reverse can mutate the original.
No. It is a stable Underscore chaining helper and still featured in the official docs.

Next: _.value()

Learn how to unwrap a chained Underscore wrapper into a plain result.

_.value() 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