Lodash _.indexOf() method
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
NaNis 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
_.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
-1if not found.
Primitive match
Numbers and strings compare by value; the first equal slot wins.
import indexOf from "lodash/indexOf";
indexOf(["a", "b", "c", "b"], "b");
// 1 Object identity
Only the same reference matches; a different object literal with identical fields is not equal.
import indexOf from "lodash/indexOf";
const marker = { id: 1 };
indexOf([marker, { id: 1 }], marker);
// 0 Start after the first match
Use fromIndex to find a later duplicate instead of slicing the array manually.
import indexOf from "lodash/indexOf";
indexOf([2, 1, 2, 3, 2], 2, 2);
// 2 📋 _.indexOf vs Array.prototype.indexOf vs _.findIndex
| API | What you pass | Note |
|---|---|---|
_.indexOf(array, value, fromIndex) | Concrete value | SameValueZero; Lodash import style; array-like inputs |
Array.prototype.indexOf | Concrete searchElement | Built-in on real arrays; same equality rules in modern engines |
_.findIndex(array, predicate) | Predicate / iteratee | Use when the condition is not a single fixed value |
Pitfalls to avoid
Branch before splice
-1 is truthy in JavaScript; always compare with === -1 (or use includes) before mutating by index.
No deep equality
Nested structures are compared by reference at the top level only; use findIndex with a custom comparator for deep matching.
First match only
To scan all occurrences, loop with a moving fromIndex or switch to a different algorithm.
❓ FAQ
Summary
- Purpose:
_.indexOf(array, value, fromIndex)returns the first index wherevaluematches (SameValueZero), or-1. - Safety: the input array is not mutated.
- Next: Lodash _.initial(), _.head() (earlier in this track), or the array methods hub.
For the last occurrence instead of the first, see _.lastIndexOf() with the same equality rules and optional fromIndex.
6 people found this page helpful
