Underscore.js _.contains() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Collection

Definition

Returns true if the value is present in the list. Arrays use indexOf internally; optional fromIndex starts the search at a given index (official Underscore docs). Stable Collection helper — not deprecated.

Pass the required arguments shown below. Underscore collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.contains. Start with this line.

Underscore
_.contains(list, value, [fromIndex])
CallWhat it does
_.contains(list, value, [fromIndex])Primary signature.
_.contains(...)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
_.contains(list, value, [fromIndex])

Parameters

ParameterDescription
listRequired. Array, array-like, or plain object
valueRequired. Value to find (===)
fromIndexOptional. Start index for the search

Return Value

Return ValueDescription
*true if found, else false

Underscore.js Examples

Example 1 — Official Docs Check

Ask whether 3 appears in [1, 2, 3].

Underscore
_.contains([1, 2, 3], 3);
// → true

_.contains([1, 2, 3], 4);
// → false
Try It Yourself

How It Works

  1. Scan. Underscore walks the array looking for an exact match.
  2. Hit. 3 === 3 → returns true.
  3. Miss. 4 is never found → false.
  4. Docs sample. First call is the official Underscore example.

Example 2 — Start at fromIndex

Skip earlier elements so only a later match counts.

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

_.contains(nums, 1);       // true  (index 0)
_.contains(nums, 1, 1);    // true  (index 2)
_.contains(nums, 1, 3);    // false (nothing after index 3)
Try It Yourself

How It Works

  1. Default. Search from index 0 — first 1 matches.
  2. fromIndex 1. Skips index 0; finds 1 at index 2.
  3. fromIndex 3. Only 3 remains — no 1 → false.
  4. Use case. “Is this value still ahead?” after a known position.

Example 3 — Search Object Values

Plain objects are checked by value, not by key name.

Underscore
var scores = { alice: 10, bob: 20, cara: 15 };

_.contains(scores, 20);      // true
_.contains(scores, "bob");   // false (keys are not searched)
_.contains(scores, 99);      // false
Try It Yourself

How It Works

  1. Values. Underscore looks at 10, 20, 15.
  2. Hit. 20 is present → true.
  3. Keys ignored. "bob" is a key, not a value → false.
  4. Need a key? Use _.has(obj, key) or "bob" in scores.

Example 4 — Aliases & Strict Equality

Aliases are identical; types and object identity still matter.

Underscore
_.include([1, 2, 3], 2);     // true  (alias)
_.includes([1, 2, 3], 2);    // true  (alias)

_.contains([1, 2, 3], "3");  // false (number ≠ string)

var a = { id: 1 };
_.contains([a], a);          // true  (same reference)
_.contains([a], { id: 1 });  // false (different object)
Try It Yourself

How It Works

  1. Aliases. include / includes call the same function as contains.
  2. Types. 3 and "3" never match under ===.
  3. Same ref. Object a in the array matches itself.
  4. Look-alike. A new { id: 1 } is a different reference → false.

Browser & Runtime Support

_.contains (aliases _.include / _.includes) 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

_.contains()

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

_.contains(list, value, [fromIndex]) returns true if value is present in the list. The official example is _.contains([1, 2, 3], 3) → true.
Yes. include and includes are aliases of contains. They call the same function.
With strict equality (===). So 3 and "3" do not match, and two objects match only if they are the same reference.
Optional start index. The search begins at that position; earlier matches are ignored. Arrays use indexOf under the hood.
_.contains checks for an exact value. _.some runs a predicate and returns true if any element passes your test.
Yes. For a plain object it searches the object’s values (not keys), so _.contains({ a: 1, b: 2 }, 2) is true.

Next: _.invoke()

Call the same method name on every item in a collection and collect the results.

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