Underscore.js _.findIndex() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Similar to _.indexOf, but returns the first index where a predicate truth test passes; otherwise -1 (official Underscore docs). Stable Array helper — not deprecated.

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

Syntax

Call _.findIndex. Start with this line.

Underscore
_.findIndex(array, predicate, [context])
CallWhat it does
_.findIndex(array, predicate, [context])Primary signature.
_.findIndex(...)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
_.findIndex(array, predicate, [context])

Parameters

ParameterDescription
arrayRequired. List to search left to right
predicateRequired. Function / iteratee; truthy = match
contextOptional. this binding for the predicate

Return Value

Return ValueDescription
*Index number, or -1

Underscore.js Examples

Example 1 — Official: First Matching Index

Find the index of the first prime number.

Underscore
function isPrime(n) {
  if (n < 2) return false;
  for (var i = 2; i * i <= n; i++) {
    if (n % i === 0) return false;
  }
  return true;
}

_.findIndex([4, 6, 7, 12], isPrime);
// → 2
Try It Yourself

How It Works

  1. Define the test. isPrime returns truthy only for primes.
  2. Scan left to right. 4 and 6 fail; 7 passes.
  3. Return early. Index 2 is returned immediately.
  4. Skip the rest. 12 is never tested.

Example 2 — Official: Nothing Matches → -1

When no item passes the predicate, Underscore returns -1.

Underscore
function isPrime(n) {
  if (n < 2) return false;
  for (var i = 2; i * i <= n; i++) {
    if (n % i === 0) return false;
  }
  return true;
}

_.findIndex([4, 6, 8, 12], isPrime);
// → -1
Try It Yourself

How It Works

  1. Test every item. 4, 6, 8, and 12 are composite.
  2. No truthy result. The loop finishes without a match.
  3. Return -1. Same convention as _.indexOf when missing.
  4. Guard. Check !== -1 before using the index.

Example 3 — Index of an Object by Predicate

Find the position of the first user with a given id.

Underscore
var users = [
  { id: 1, name: "Ada" },
  { id: 2, name: "Grace" },
  { id: 3, name: "Alan" }
];

_.findIndex(users, function (user) {
  return user.id === 3;
});
// → 2
Try It Yourself

How It Works

  1. Objects need a predicate. _.indexOf would compare by reference and fail.
  2. Check each user. Ids 1 and 2 fail; 3 passes.
  3. Return the index. Useful for splice or in-place updates.
  4. Want the object? Use _.find instead.

Example 4 — Matcher Object Shorthand

Underscore iteratees let you pass a properties object instead of a function.

Underscore
var users = [
  { id: 1, name: "Ada" },
  { id: 2, name: "Grace" },
  { id: 3, name: "Alan" }
];

_.findIndex(users, { id: 2 });
// → 1
Try It Yourself

How It Works

  1. Matcher shorthand. { id: 2 } becomes a predicate that checks those properties.
  2. First match. Grace has id: 2 at index 1.
  3. Same idea as findWhere. See _.findWhere for the element itself.
  4. Still returns -1 when no object matches the properties.

Browser & Runtime Support

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

_.findIndex()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded. Native Array.prototype.findIndex is similar but separate.

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
_.findIndex Underscore 1.x+

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

Frequently Asked Questions

_.findIndex(array, predicate, [context]) returns the first index where the predicate truth test passes; otherwise -1. Official example: _.findIndex([4, 6, 7, 12], isPrime) → 2.
It returns -1. Check with index !== -1 before using the index to update or splice.
_.indexOf looks for a specific value with ===. _.findIndex runs a predicate against each item and returns the index of the first that passes.
_.find returns the matching element; _.findIndex returns its position. Use findIndex when you need the index.
Yes. Pass a predicate like function (user) { return user.id === 3; }, or an iteratee shorthand such as { id: 3 }.
No. It only reads the array and returns a number.

Next: _.findLastIndex()

Search from the end — get the last index where a predicate passes.

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