Lodash _.cond() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Util

Definition

Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy. Part of Lodash Util since 4.0.0 (stable — not deprecated).

Note: Predicates and handlers receive the same this binding and arguments as the created function.

Syntax

Call _.cond. Start with this line.

Lodash
_.cond(pairs)
CallWhat it does
_.cond(pairs)Primary signature.
_.cond(...)Call the util helper.

Prefer import cond from "lodash/cond" when you want a smaller bundle.

Library declarations
Lodash
_.cond(pairs)

Parameters

ParameterDescription
pairsRequired. Array of [predicate, function] pairs

Return Value

Return ValueDescription
*New composite function

Lodash Examples

Example 1 — Official: Three Branches

Match A, then B, else a default string.

Lodash
var func = _.cond([
  [_.matches({ a: 1 }), _.constant('matches A')],
  [_.conforms({ b: _.isNumber }), _.constant('matches B')],
  [_.stubTrue, _.constant('no match')]
]);

console.log(func({ a: 1, b: 2 }));
// → 'matches A'

console.log(func({ a: 0, b: 1 }));
// → 'matches B'

console.log(func({ a: '1', b: '2' }));
// → 'no match'
Try It Yourself

How It Works

  1. Build. Three pairs: matches, conforms, stubTrue.
  2. First call. a: 1 matches → 'matches A'.
  3. Second. No match on a, but b is a number → 'matches B'.
  4. Third. Neither rule fits → default 'no match'.

Example 2 — _.matches Branch

Stop early when a partial object match succeeds.

Lodash
var func = _.cond([
  [_.matches({ a: 1 }), _.constant('matches A')],
  [_.stubTrue, _.constant('other')]
]);

console.log(func({ a: 1, b: 9 }));
// → 'matches A'

console.log(func({ a: 2 }));
// → 'other'
Try It Yourself

How It Works

  1. Predicate. _.matches({ a: 1 }) checks a partial deep match.
  2. Hit. { a: 1, b: 9 } has a: 1 → first handler.
  3. Miss. { a: 2 } fails the match.
  4. Default. _.stubTrue runs → 'other'.

Example 3 — _.conforms Branch

Test property values with predicates like _.isNumber.

Lodash
var func = _.cond([
  [_.conforms({ b: _.isNumber }), _.constant('matches B')],
  [_.stubTrue, _.constant('no match')]
]);

console.log(func({ a: 0, b: 1 }));
// → 'matches B'

console.log(func({ a: '1', b: '2' }));
// → 'no match'
Try It Yourself

How It Works

  1. Spec. _.conforms({ b: _.isNumber }) checks property b.
  2. Number. b: 1 passes → 'matches B'.
  3. String. b: '2' fails _.isNumber.
  4. Fallback. Default pair returns 'no match'.

Example 4 — Default with _.stubTrue

Without a default, a miss returns undefined.

Lodash
var withDefault = _.cond([
  [function (n) { return n > 10; }, _.constant('big')],
  [_.stubTrue, _.constant('small')]
]);

var noDefault = _.cond([
  [function (n) { return n > 10; }, _.constant('big')]
]);

console.log(withDefault(3));  // 'small'
console.log(noDefault(3));    // undefined
Try It Yourself

How It Works

  1. Custom check. n > 10 fails for 3.
  2. With stubTrue. Default handler returns 'small'.
  3. Without. No pair matches → undefined.
  4. Tip. Prefer an explicit default for clear APIs.

Example 5 — Order Matters

First truthy predicate wins — put specific rules first.

Lodash
var wrong = _.cond([
  [function (n) { return n > 0; }, _.constant('positive')],
  [function (n) { return n > 10; }, _.constant('big')]
]);

var right = _.cond([
  [function (n) { return n > 10; }, _.constant('big')],
  [function (n) { return n > 0; }, _.constant('positive')],
  [_.stubTrue, _.constant('other')]
]);

console.log(wrong(15));  // 'positive'  (too broad first)
console.log(right(15));  // 'big'
Try It Yourself

How It Works

  1. Wrong order. n > 0 matches 15 first.
  2. Never reaches. The n > 10 rule is skipped.
  3. Right order. Specific > 10 comes before > 0.
  4. Rule. Narrow predicates before broad ones.

Browser & Runtime Support

_.cond is a Lodash Util helper since 4.0.0. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.0.0+

_.cond()

Available in [email protected] full builds and the lodash/cond modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.0+
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
_.cond Lodash 4.0.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import cond from "lodash/cond" for smaller bundles. Pair with matches, conforms, constant, and stubTrue for clear tables of rules.

Frequently Asked Questions

_.cond(pairs) returns a function that runs each [predicate, handler] pair in order and invokes the handler of the first truthy predicate. Official example returns 'matches A', 'matches B', or 'no match'.
Both receive the same this binding and arguments passed to the created function. If you call func(obj), every predicate and the chosen handler see obj.
_.stubTrue always returns true, so it acts as a default branch—like else or switch default. Without a match, the cond function returns undefined.
Yes. Lodash stops at the first truthy predicate. Put specific rules before general ones.
switch compares one value to case labels. _.cond runs arbitrary predicates—ranges, type checks, object shapes—in a reusable function.
Use import cond from "lodash/cond" or require("lodash/cond"). Full build: import _ from "lodash" then _.cond.

Next: _.conforms()

Build a predicate that checks object properties against functions.

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