Underscore.js _.indexOf() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Returns the index at which value can be found in the array, or -1 if it is not present (official Underscore docs). Optional third argument: true for sorted binary search, or a from-index number.

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

Syntax

Call _.indexOf. Start with this line.

Underscore
_.indexOf(array, value, [isSorted])
CallWhat it does
_.indexOf(array, value, [isSorted])Primary signature.
_.indexOf(...)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
_.indexOf(array, value, [isSorted])

Parameters

ParameterDescription
arrayRequired. List to search
valueRequired. Target to find (strict ===)
isSorted / fromIndexOptional. true = binary search; number = start index

Return Value

Return ValueDescription
*Index number, or -1

Underscore.js Examples

Example 1 — Official: Find a Value

Return the first index where the value appears.

Underscore
_.indexOf([1, 2, 3], 2);
// → 1
Try It Yourself

How It Works

  1. Source. Three numbers at indexes 0, 1, 2.
  2. Scan. Compare with === from the start.
  3. Hit. Value 2 sits at index 1.
  4. Stop. Return immediately — no need to check further.

Example 2 — Not Found Returns -1

Missing values do not throw — you get -1.

Underscore
var fruits = ["apple", "banana", "cherry"];

_.indexOf(fruits, "banana");  // → 1
_.indexOf(fruits, "grape");   // → -1
Try It Yourself

How It Works

  1. Hit. "banana" is at index 1.
  2. Miss. "grape" is not in the list.
  3. Signal. Underscore returns -1.
  4. Safe use. Always check index !== -1 before reading fruits[index].

Example 3 — Start from an Index

Pass a number as the third argument to skip earlier matches.

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

_.indexOf(nums, 1);     // → 0
_.indexOf(nums, 1, 1);  // → 2
Try It Yourself

How It Works

  1. Default. First 1 is at index 0.
  2. fromIndex 1. Search begins at slot 1 (2).
  3. Next hit. The following 1 is at index 2.
  4. Use case. Find the next occurrence after a known position.

Example 4 — Sorted Binary Search

Pass true when the array is already sorted for a faster search.

Underscore
var sorted = [10, 20, 30, 40, 50, 60];

_.indexOf(sorted, 40, true);
// → 3
Try It Yourself

How It Works

  1. Sorted input. Values increase left to right.
  2. true. Underscore uses binary search (and <), not a full linear scan.
  3. Hit. 40 is at index 3.
  4. Warning. If the array is not sorted, results can be wrong — only pass true when order is guaranteed.

Browser & Runtime Support

_.indexOf ships with Underscore.js (1.x), including sorted binary search and fromIndex. It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore. Native Array.prototype.indexOf is separate and has no isSorted flag.

✓ Stable · Underscore 1.x

_.indexOf()

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

_.indexOf(array, value, [isSorted]) returns the first index where value is found, or -1 if it is missing. Comparison uses strict === (with special NaN handling).
Pass true to binary-search a sorted array (faster on large sorted lists). Pass a number to start the linear search at that index (fromIndex).
It returns -1. Check with if (index !== -1) before using the index.
_.indexOf looks for a fixed value with ===. _.findIndex runs a predicate function and returns the first index that passes the test.
_.indexOf finds the first match from the start. _.lastIndexOf finds the last match from the end.
No. It only reads and returns a number.

Next: _.lastIndexOf()

Find the last occurrence of a value — with an optional fromIndex.

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

11 people found this page helpful