JavaScript Array.prototype.lastIndexOf() Method
The lastIndexOf() method returns the last index at which a value is found in an array, or -1.
Description
The lastIndexOf() method searches the array backwards and returns the index of the last occurrence of a value, using strict equality, or -1 if it isn't found.
Like indexOf(), it compares values with ===, so it can't find NaN and only finds objects that are the same reference. Empty slots in sparse arrays are skipped. The optional second argument sets where to start searching backwards from, and negative values count from the end.
A common use is checking whether a value appears more than once: if indexOf(x) and lastIndexOf(x) give different positions, it's a duplicate. To search backwards with a condition instead of an exact value, use findLastIndex().
Syntax
array.lastIndexOf(searchElement)
array.lastIndexOf(searchElement, fromIndex)Parameters
- searchElementThe value to look for.
- fromIndexOptional. The index to start searching backwards from.
Return Value
The last index of the value, or -1 if it isn't found.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.lastIndexOf() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





