Array.prototype.lastIndexOf()

Returns the last index at which a given element is found, searching backwards.

Since ES5 Spec ↗

Syntax

arr.lastIndexOf(searchElement, fromIndex)

Parameters

NameTypeRequiredDescription
searchElement any Yes The value to locate, compared using strict equality (===).
fromIndex number No Index to start searching backwards from. Negative values count from the end. Defaults to the last index.

Returns

number — The last matching index, or -1 if not found.

Examples

console.log(['a', 'b', 'c', 'b'].lastIndexOf('b'));
Output
3
console.log([1, 2, 3].lastIndexOf(9));
Output
-1

Notes

Searches the array in reverse but returns the absolute (forward) index. Uses strict equality and cannot find NaN.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.5
safari 3.0
edge 12
node 0.10

See also