Lodash _.chain() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Seq & chaining

What You’ll Learn

By the end of this tutorial, you’ll use _.chain() to build readable multi-step Lodash pipelines and unwrap results with .value().

01

Core syntax

_.chain(value) starts a wrapper.

02

Fluent steps

Chain map, filter, sortBy, pick, etc.

03

Always unwrap

Finish with .value().

04

Readable pipelines

Top-to-bottom data flow.

05

vs nested calls

Compare with _.filter(_.map(...)).

06

Seq family

Pair with _.tap() and _.thru().

What Is _.chain()?

_.chain() wraps any value in a Lodash sequence object so you can call chainable Lodash methods one after another in a fluent, top-to-bottom pipeline. It is the explicit entry point for method chaining—the same role as the shorthand _(value).

💡
Wrapper, not result

After _.chain([1,2,3]).map(...) you still hold a wrapper. Call .value() to get the plain array, object, or number your pipeline produced.

Use it when three or more Lodash steps would otherwise nest inside each other, when you want pipeline-style readability, or when mixing Collection, Array, and Object helpers in one flow.

📝 Syntax

The signature takes one argument—the value to wrap:

javascript
_.chain(value)

Syntax Rules

  • value — any value to wrap (array, object, number, etc.).
  • Return value — a Lodash wrapper; chainable methods return another wrapper.
  • Unwrap — call .value() or .valueOf() for the final result.
  • Chainable methods — most Lodash map/filter/pick-style helpers work on the wrapper.
  • Not lazy — steps run when you unwrap, not as an infinite stream.
javascript
import chain from "lodash/chain";

const data = [1, 2, 3, 4, 5];
const result = chain(data)
  .map(function (x) { return x * 2; })
  .filter(function (x) { return x > 5; })
  .value();

// result -> [6, 8, 10]

⚡ Quick Reference

TaskCode patternResult
Start a pipeline_.chain(array)Lodash wrapper
Map then filter_.chain(x).map(...).filter(...)Still a wrapper
Get final result... .value()Plain JS value
Shorthand wrap_(value)See _(value)
Side-effect peek.tap(fn)See _.tap()
Custom transform.thru(fn)See _.thru()
Returns
Wrapper

Until .value()

Unwrap
.value()

Required finish

Alias
_(x)

Same entry point

🧰 Parameters

The single argument to _.chain() and what the pipeline returns:

value Required

The initial value wrapped in a Lodash sequence (array, object, etc.).

_.chain([1, 2, 3, 4, 5])
return value Wrapper

A Lodash wrapper exposing chainable methods until you call .value().

_.chain(x).map(...)
.value() Unwrap

Executes the pipeline and returns the plain JavaScript result.

.value() // -> [6, 8, 10]
chain steps Middle

Any chainable Lodash method—map, filter, sortBy, pick, groupBy, etc.

.filter(...).sortBy(...)

Import the main lodash package or ensure chain-compatible method bundles are available in your build.

Examples Gallery

Practical _.chain() patterns with sample output and interactive Try It Yourself labs.

📚 Getting Started

Wrap a value, chain Lodash methods, unwrap with .value().

Example 1 — Basic map and filter chain

Double each number, keep values greater than 5, then unwrap.

javascript
const data = [1, 2, 3, 4, 5];

const result = _.chain(data)
  .map(function (x) { return x * 2; })
  .filter(function (x) { return x > 5; })
  .value();

console.log(result);
// -> [6, 8, 10]
Try It Yourself

How It Works

_.chain wraps the array; each method returns another wrapper until .value() runs the pipeline.

Example 2 — Filter, sort, and map user records

Process API-style user data in one readable pipeline.

javascript
const userData = [
  { firstName: "Ada", lastName: "Lovelace", age: 36 },
  { firstName: "Grace", lastName: "Hopper", age: 85 },
  { firstName: "Tim", lastName: "Berners-Lee", age: 16 }
];

const processed = _.chain(userData)
  .filter(function (user) { return user.age >= 18; })
  .sortBy("lastName")
  .map(function (user) {
    return {
      fullName: user.firstName + " " + user.lastName,
      age: user.age
    };
  })
  .value();

console.log(processed);
// -> [{ fullName: "Ada Lovelace", age: 36 }, { fullName: "Grace Hopper", age: 85 }]
Try It Yourself

📈 Practical Patterns

Unwrapping, limiting results, aggregation, and nested-call comparison.

Example 3 — Forgetting .value() returns a wrapper

Without .value(), you do not get a plain array—a common beginner mistake.

javascript
const wrapped = _.chain([1, 2, 3]).map(function (n) { return n + 1; });

console.log(Array.isArray(wrapped));
// -> false

const plain = wrapped.value();
console.log(plain);
// -> [2, 3, 4]
Try It Yourself

Example 4 — Keep chains concise with take

Limit output after transform steps to avoid overly long pipelines.

javascript
const data = [1, 2, 3, 4, 5];

const conciseResult = _.chain(data)
  .map(function (x) { return x * 2; })
  .take(3)
  .value();

console.log(conciseResult);
// -> [2, 4, 6]

Example 5 — Group and summarize sales data

Chain grouping and aggregation for dashboard-style summaries.

javascript
const salesData = [
  { product: "widget", quantity: 2, price: 10 },
  { product: "widget", quantity: 1, price: 10 },
  { product: "gadget", quantity: 3, price: 15 }
];

const summary = _.chain(salesData)
  .groupBy("product")
  .mapValues(function (products) {
    return {
      totalSales: _.sumBy(products, "quantity"),
      averagePrice: _.meanBy(products, "price")
    };
  })
  .value();

console.log(summary);
// widget: { totalSales: 3, averagePrice: 10 }
// gadget: { totalSales: 3, averagePrice: 15 }

🚀 Beyond the Basics

Chain vs nested plain Lodash calls.

Example 6 — _.chain() vs nested calls

Both approaches produce the same result; chaining reads top-to-bottom.

javascript
const data = [1, 2, 3, 4, 5];

const chained = _.chain(data)
  .map(function (x) { return x * 2; })
  .filter(function (x) { return x > 5; })
  .value();

const nested = _.filter(
  _.map(data, function (x) { return x * 2; }),
  function (x) { return x > 5; }
);

console.log(JSON.stringify(chained) === JSON.stringify(nested));
// -> true

🧠 How _.chain() Works

1

Wrap the value

_.chain(value) stores your array, object, or primitive inside a Lodash sequence wrapper.

Input
2

Queue chain steps

Each chained method (map, filter, pick, etc.) returns another wrapper with the next step recorded.

Pipeline
3

Execute on unwrap

Calling .value() runs all queued operations in order and returns plain JavaScript data.

Unwrap
=

Plain result

You get an array, object, or number—not a wrapper. Use _.tap() to inspect mid-chain without breaking the flow.

📝 Notes

  • _.chain() returns a wrapper, not the transformed data—always call .value() (or .valueOf()) to finish.
  • _(value) is a shorthand alias for starting the same kind of sequence.
  • Chaining is about readability, not lazy infinite streams—steps run when you unwrap.
  • Some chain methods (like reverse) may mutate the wrapped value in place; check docs for each method.
  • For side effects between steps (logging, debugging), use _.tap(); to transform the wrapped value inline, use _.thru().
  • For one or two Lodash calls, nested plain calls are often simpler and tree-shake better in modern bundles.

Conclusion

_.chain() turns a series of Lodash operations into a readable top-to-bottom pipeline. Wrap your data, chain the transforms you need, and unwrap with .value() to get a plain result. It shines when you have three or more sequential Lodash steps on the same value.

For the same behavior with shorter syntax, use _(value). Next in the series: the shorthand wrapper entry point and wrapper prototype helpers like .value().

💡 Best Practices

✅ Do

  • End every chain with .value() before passing data to other code
  • Keep pipelines focused—extract very long chains into named functions
  • Use take, slice, or early filter to limit intermediate work
  • Prefer chaining when you have three or more Lodash steps on one value
  • Use _.tap() for debug logging without altering the pipeline result

❌ Don’t

  • Forget .value() and treat the wrapper like a plain array or object
  • Build chains so long they become hard to test or debug
  • Assume chaining is lazy streaming—unwrap still runs steps eagerly
  • Chain when a single _.map() or native .filter() is enough
  • Mix chain wrappers with plain Lodash calls without unwrapping first

Key Takeaways

Knowledge Unlocked

Five things to remember about _.chain()

Use these when building fluent Lodash pipelines.

5
Core concepts
🔀 02

Fluent steps

map, filter, sort.

Pattern
📦 03

Always unwrap

.value() required.

Critical
📈 04

Readable flow

Top-to-bottom.

Style
🛠️ 05

Know when

3+ Lodash steps.

Guideline

❓ Frequently Asked Questions

_.chain(value) wraps a value in a Lodash sequence object so you can call chainable Lodash methods (map, filter, pick, etc.) in a fluent pipeline, then unwrap with .value().
Intermediate chain steps return a wrapper, not plain JavaScript data. .value() executes the pipeline and returns the final unwrapped result.
Both start a Lodash wrapper around a value. _.chain(value) is explicit; _(value) is the shorthand alias—they behave the same for beginning a sequence.
It depends on the methods you call in the chain. _.chain itself only wraps; map and filter return new data, while some methods like reverse may mutate in place.
Not in the general sense. The wrapper defers unwrapping until .value(), but when you unwrap, the steps run eagerly. It is not infinite lazy evaluation like a generator stream.
For one or two Lodash calls, nested plain calls or native array methods are often clearer and tree-shake better. Use chaining when you have three or more sequential Lodash steps.
Did you know?

_.chain() and _(value) start the same wrapper type—only the syntax differs. Lodash also supports implicit chaining on some methods when you use _(value) without an explicit _.chain() call, but explicit chaining makes the pipeline obvious in code reviews.

Practice _.chain() in the Live Editor

Build map/filter pipelines, process user data, and learn when to call .value().

Open Try It editor →

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.

6 people found this page helpful