Lodash _.lastIndexOf() method
What you’ll learn
- How
_.lastIndexOf(array, value, [fromIndex])returns the last matching index or-1. - Why it pairs with _.indexOf() when duplicates exist and you need the tail occurrence.
- How optional
fromIndexbounds the backward scan without slicing the array. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.indexOf() first for SameValueZero rules; optional _.last() refresher on end-of-array indexing.
- You know that
-1means “not found” and remains truthy in conditionals. - You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.lastIndexOf answers “where is the final occurrence of this value?” by scanning from the end. It mirrors Array.prototype.lastIndexOf with the same equality rules as _.indexOf, packaged for consistent Lodash imports.
Backward scan
Duplicates resolve to the rightmost matching index by default.
fromIndex
Clamp the search window when you only care about a suffix of the array.
Non-destructive
The collection is read-only; you receive an index or -1.
Syntax
_.lastIndexOf(array, value, [fromIndex=array.length-1]) - array: collection to search (array-like values are coerced).
- value: the element to locate using SameValueZero comparison.
- fromIndex: optional index where the backward search begins (defaults to the last occupied index).
- Returns: the last matching index at or before the effective start, or
-1if not found.
Last duplicate wins
When the same primitive appears multiple times, lastIndexOf returns the rightmost index.
import lastIndexOf from "lodash/lastIndexOf";
lastIndexOf([2, 1, 2, 3, 2], 2);
// 4 Not found
Missing values yield -1, the same sentinel as indexOf.
import lastIndexOf from "lodash/lastIndexOf";
lastIndexOf([1, 2, 3], 9);
// -1 Bound the backward scan
Pass fromIndex to ignore matches after a position—here the final 2 at index 4 is skipped, so the answer is 2.
import lastIndexOf from "lodash/lastIndexOf";
lastIndexOf([2, 1, 2, 3, 2], 2, 3);
// 2 📋 _.lastIndexOf vs Array.prototype.lastIndexOf vs _.indexOf
| API | Scan direction | Note |
|---|---|---|
_.lastIndexOf(array, value, fromIndex) | Right to left | SameValueZero; Lodash import style; array-like inputs |
Array.prototype.lastIndexOf | Right to left | Built-in on real arrays; same semantics in modern engines |
_.indexOf(array, value, fromIndex) | Left to right | Returns the first match instead of the last |
Pitfalls to avoid
Truthy sentinel
-1 is truthy; compare with === -1 or use includes before splicing or slicing by index.
Objects compare by identity
Separate object literals never match; pass the same reference you stored in the array.
Not a predicate search
For “last element where fn(item) is true,” use _.findLastIndex() instead of trying to encode logic into value.
❓ FAQ
Summary
- Purpose:
_.lastIndexOf(array, value, fromIndex)returns the last index wherevaluematches (SameValueZero), or-1. - Pairs with: _.indexOf() for first vs last duplicate handling.
- Next: Lodash _.nth(), _.last() (earlier in this track), or the array methods hub.
When the match is defined by a predicate instead of one fixed value, use _.findLastIndex() to scan from the end with custom logic.
6 people found this page helpful
