Lodash _.indexOf() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.indexOf(array, value, [fromIndex=0]) returns the first matching index or -1.
  • Why reference equality matters for objects and how NaN is handled under SameValueZero.
  • How this differs from _.findIndex() when you only need to locate one known value.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Optional prior read _.head(); you should know that array indexes start at 0 and that -1 often means “not found.”

  • You understand that two distinct object literals {} and {} are never the same reference unless assigned from the same variable.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.indexOf answers “where is this exact value?” by scanning left to right. It is the Lodash twin of Array.prototype.indexOf with the same familiar rules, packaged for consistent imports next to other collection helpers.

Value search

You pass the element to locate, not a predicate callback.

fromIndex

Skip early matches when you need the next duplicate or a bounded scan window.

Non-destructive

The array is read-only; you only receive an index or -1.

Syntax

javascript
_.indexOf(array, value, [fromIndex=0])
  • array: collection to search (array-like values are coerced).
  • value: the element to locate using SameValueZero comparison.
  • fromIndex: optional starting index for the forward scan (defaults to 0).
  • Returns: the first matching index, or -1 if not found.
1

Primitive match

Numbers and strings compare by value; the first equal slot wins.

javascript
import indexOf from "lodash/indexOf";

indexOf(["a", "b", "c", "b"], "b");
// 1
Try it Yourself
2

Object identity

Only the same reference matches; a different object literal with identical fields is not equal.

javascript
import indexOf from "lodash/indexOf";

const marker = { id: 1 };
indexOf([marker, { id: 1 }], marker);
// 0
Try it Yourself
3

Start after the first match

Use fromIndex to find a later duplicate instead of slicing the array manually.

javascript
import indexOf from "lodash/indexOf";

indexOf([2, 1, 2, 3, 2], 2, 2);
// 2
Try it Yourself

📋 _.indexOf vs Array.prototype.indexOf vs _.findIndex

APIWhat you passNote
_.indexOf(array, value, fromIndex)Concrete valueSameValueZero; Lodash import style; array-like inputs
Array.prototype.indexOfConcrete searchElementBuilt-in on real arrays; same equality rules in modern engines
_.findIndex(array, predicate)Predicate / iterateeUse when the condition is not a single fixed value

Pitfalls to avoid

-1

Branch before splice

-1 is truthy in JavaScript; always compare with === -1 (or use includes) before mutating by index.

Deep

No deep equality

Nested structures are compared by reference at the top level only; use findIndex with a custom comparator for deep matching.

Order

First match only

To scan all occurrences, loop with a moving fromIndex or switch to a different algorithm.

❓ FAQ

No. It scans for a match and returns an index (or -1). The source array is unchanged.
Lodash aligns with SameValueZero semantics for this search, like native Array.prototype.indexOf in modern engines: NaN matches NaN, and +0 matches -0 for the search value.
It is the starting offset for the forward scan (with Lodash normalization for negative values). The first match at or after that index wins.
indexOf looks for one concrete value with SameValueZero. findIndex runs a predicate function per element and can express arbitrary logic.
Use import indexOf from "lodash/indexOf" or require("lodash/indexOf") for tree-shaking friendly bundles.

Summary

  • Purpose: _.indexOf(array, value, fromIndex) returns the first index where value matches (SameValueZero), or -1.
  • Safety: the input array is not mutated.
  • Next: Lodash _.initial(), _.head() (earlier in this track), or the array methods hub.
Did you know?

For the last occurrence instead of the first, see _.lastIndexOf() with the same equality rules and optional fromIndex.

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.

6 people found this page helpful