Array.prototype.lastIndexOf()
Returns the last index at which a value is found, searching backwards from the end.
lastIndexOf(searchElement)
lastIndexOf(searchElement, fromIndex)- Added in JavaScript ES5 (2009)
Parameters
| Parameter | Type | Description |
|---|---|---|
$searchElement | any | The value to locate, compared using strict equality (===). |
$fromIndexoptional | integer= array.length - 1 | Zero-based index to start searching backwards from, converted to an integer. A negative value counts back from the end (fromIndex + array.length); if that result is negative, or fromIndex is below -array.length, the array is not searched and -1 is returned. Values at or above array.length search the whole array. |
Return value
numberThe last index at which searchElement is found, or -1 if it is not present.
lastIndexOf() is indexOf() in reverse: it starts at the end (or at fromIndex) and works backwards, returning the highest index that strictly equals searchElement, or -1.
When to reach for it
Reach for it when duplicates exist and the last one is the one you want: the most recent entry in an append-only list, the final separator in a split token array, or the last checkpoint before the end. For predicate matching rather than value equality, use findLastIndex().
The fromIndex asymmetry
This is where bugs come from. fromIndex defaults to array.length - 1, not 0, and it marks where the backwards scan begins — everything after it is ignored. So [2,5,9,2].lastIndexOf(2, 2) returns 0, not 3. Negative values resolve as fromIndex + length, and if that lands below zero the array is not searched at all, unlike indexOf(), which clamps to 0 and searches everything.
Other gotchas
Strict equality means NaN is never found, and empty slots in sparse arrays are skipped. -1 is truthy, so always compare with !== -1.
Example
When to use it
- Split a filename on its final dot to get the extension
- Find the most recent occurrence of a status in an append-only log
- Locate the last separator in a path-like token array
- Detect duplicates by comparing indexOf() and lastIndexOf() results
Frequently asked questions
What is the default fromIndex for lastIndexOf()?
array.length - 1, meaning the search starts at the last element and moves backwards. That is the opposite of indexOf(), whose default is 0.
Why does lastIndexOf() return -1 with a negative fromIndex?
Because a negative fromIndex resolves to fromIndex + array.length, and if that result is still negative the array is not searched at all. indexOf() clamps the same input to 0 and searches everything, so the two methods behave differently here.
How do I find the last element matching a condition rather than a value?
Use findLastIndex() for the index or findLast() for the element. lastIndexOf() only does strict-equality comparison against a value, so it cannot match on object properties or find NaN.
More examples
fromIndex counts backwards from where you start
Prints 3, 0, 0, -1. Starting at index 2 excludes the 2 at index 3, so the earlier one is returned. -2 resolves to index 2 and behaves the same, while -5 resolves below zero and the array is never searched — indexOf would have clamped to 0 instead.
const nums = [2, 5, 9, 2];
console.log(nums.lastIndexOf(2, 3));
console.log(nums.lastIndexOf(2, 2));
console.log(nums.lastIndexOf(2, -2));
console.log(nums.lastIndexOf(2, -5));Splitting on the final separator
Prints the three parts, then 2, then 'tar.gz'. The last line prints true: when the first and last index of a value differ, that value appears more than once — a cheap duplicate check for primitives.
const parts = "archive.tar.gz".split(".");
console.log(parts);
console.log(parts.lastIndexOf("gz"));
console.log(parts.slice(parts.lastIndexOf("tar")).join("."));
const dupes = [1, 2, 1];
console.log(dupes.indexOf(1) !== dupes.lastIndexOf(1));
Discussion