Lodash _.indexOf() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Gets the index of the first occurrence of value in array using SameValueZero equality. Returns -1 if not found. Part of Lodash 4.x Array.

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

Syntax

Call _.indexOf on an array. Start with this line.

Lodash
_.indexOf(array, value, [fromIndex=0])
CallWhat it does
_.indexOf(array, value, [fromIndex=0])Primary signature.
_.indexOf(array)Call with an array argument.

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

Library declarations
Lodash
_.indexOf(array, value, [fromIndex=0])

Parameters

ParameterDescription
arrayRequired. Collection to inspect
valueRequired. Value to search for
fromIndexOptional. Start index; default 0

Return Value

Return ValueDescription
*Index number, or -1

Lodash Examples

Example 1 — Official: First Occurrence

Scan from the start until the first match.

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

How It Works

  1. Source. Official sample: [1, 2, 1, 2].
  2. Target. Look for 2 from index 0.
  3. First hit. Index 1 matches — search stops.
  4. Later 2. Index 3 is ignored because the first match already won.

Example 2 — Official: Search from fromIndex

Skip earlier elements by passing a start index.

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

How It Works

  1. Start at 2. Indexes 0 and 1 are skipped.
  2. Scan. Index 2 is 1 — no match.
  3. Hit. Index 3 is 2 → return 3.
  4. Use. Find the next occurrence after a known position.

Example 3 — Value Not Found

Missing values return -1, not an error.

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

How It Works

  1. Target. Look for 3.
  2. Full scan. Every element is checked; none match.
  3. Result. Lodash returns -1.
  4. Guard. Use if (i === -1) before indexing with the result.

Example 4 — Find NaN (SameValueZero)

Lodash can locate NaN; native indexOf cannot.

Lodash
_.indexOf([1, 2, NaN], NaN);
// → 2

[1, 2, NaN].indexOf(NaN);
// → -1  (native === fails for NaN)
Try It Yourself

How It Works

  1. SameValueZero. Lodash treats NaN as equal to NaN.
  2. Match. Index 2 is found.
  3. Native contrast. Array#indexOf uses ===, so NaN never matches.
  4. Rule. Prefer Lodash (or includes) when searching for NaN.

Example 5 — Negative fromIndex

A negative start is counted from the end of the array.

Lodash
_.indexOf([1, 2, 1, 2], 1, -2);
// → 2
// length 4; -2 + 4 = start at index 2
Try It Yourself

How It Works

  1. Offset. -2 + 4 = 2 — search starts at index 2.
  2. Slot 2. Value is 1 → immediate match.
  3. Earlier 1. Index 0 is never visited.
  4. Tip. Negative starts still scan forward from that point.

Browser & Runtime Support

_.indexOf is a Lodash 4.x Array helper (available since early Lodash). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.x

_.indexOf()

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

100% With Lodash 4.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 Lodash 4.x+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import indexOf from "lodash/indexOf" for smaller builds. Native Array.prototype.indexOf is similar but cannot find NaN.

Frequently Asked Questions

_.indexOf(array, value, fromIndex) returns the index of the first occurrence of value in array using SameValueZero equality. If not found, it returns -1.
No. It only scans and returns a number. The source array is unchanged.
It is where the forward search starts (default 0). A negative fromIndex is treated as an offset from the end of the array.
Yes. Lodash uses SameValueZero, so _.indexOf([NaN], NaN) returns 0. Native Array.prototype.indexOf cannot find NaN.
indexOf matches one concrete value. findIndex runs a predicate function per element for custom logic.
Use import indexOf from "lodash/indexOf" or require("lodash/indexOf") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.indexOf.

Next: _.initial()

Get all elements of an array except the last one.

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