Array.prototype.lastIndexOf()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
ThelastIndexOf()
method ofArray
instances returns the last index at whicha given element can be found in the array, or -1 if it is not present. The array issearched backwards, starting atfromIndex
.
Try it
const animals = ["Dodo", "Tiger", "Penguin", "Dodo"];console.log(animals.lastIndexOf("Dodo"));// Expected output: 3console.log(animals.lastIndexOf("Tiger"));// Expected output: 1
Syntax
lastIndexOf(searchElement)lastIndexOf(searchElement, fromIndex)
Parameters
searchElement
Element to locate in the array.
fromIndex
OptionalZero-based index at which to start searching backwards,converted to an integer.
- Negative index counts back from the end of the array — if
-array.length <= fromIndex < 0
,fromIndex + array.length
is used. - If
fromIndex < -array.length
, the array is not searched and-1
is returned. You can think of it conceptually as starting at a nonexistent position before the beginning of the array and going backwards from there. There are no array elements on the way, sosearchElement
is never found. - If
fromIndex >= array.length
orfromIndex
is omitted orundefined
,array.length - 1
is used, causing the entire array to be searched. You can think of it conceptually as starting at a nonexistent position beyond the end of the array and going backwards from there. It eventually reaches the real end position of the array, at which point it starts searching backwards through the actual array elements.
- Negative index counts back from the end of the array — if
Return value
The last index ofsearchElement
in the array;-1
if not found.
Description
ThelastIndexOf()
method comparessearchElement
to elements of the array usingstrict equality (the same algorithm used by the===
operator).NaN
values are never compared as equal, solastIndexOf()
always returns-1
whensearchElement
isNaN
.
ThelastIndexOf()
method skips empty slots insparse arrays.
ThelastIndexOf()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties.
Examples
Using lastIndexOf()
The following example useslastIndexOf()
to locate values in an array.
const numbers = [2, 5, 9, 2];numbers.lastIndexOf(2); // 3numbers.lastIndexOf(7); // -1numbers.lastIndexOf(2, 3); // 3numbers.lastIndexOf(2, 2); // 0numbers.lastIndexOf(2, -2); // 0numbers.lastIndexOf(2, -1); // 3
You cannot uselastIndexOf()
to search forNaN
.
const array = [NaN];array.lastIndexOf(NaN); // -1
Finding all the occurrences of an element
The following example useslastIndexOf
to find all the indices of anelement in a given array, usingpush()
to add themto another array as they are found.
const indices = [];const array = ["a", "b", "a", "c", "a", "d"];const element = "a";let idx = array.lastIndexOf(element);while (idx !== -1) { indices.push(idx); idx = idx > 0 ? array.lastIndexOf(element, idx - 1) : -1;}console.log(indices);// [4, 2, 0]
Note that we have to handle the caseidx === 0
separately here because theelement will always be found regardless of thefromIndex
parameter if it isthe first element of the array. This is different from theindexOf()
method.
Using lastIndexOf() on sparse arrays
You cannot uselastIndexOf()
to search for empty slots in sparse arrays.
console.log([1, , 3].lastIndexOf(undefined)); // -1
Calling lastIndexOf() on non-array objects
ThelastIndexOf()
method reads thelength
property ofthis
and then accesses each property whose key is a nonnegative integer less thanlength
.
const arrayLike = { length: 3, 0: 2, 1: 3, 2: 2, 3: 5, // ignored by lastIndexOf() since length is 3};console.log(Array.prototype.lastIndexOf.call(arrayLike, 2));// 2console.log(Array.prototype.lastIndexOf.call(arrayLike, 5));// -1
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.lastindexof |