Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Array.prototype.lastIndexOf

Finds a value from the end of an array and returns its index.

Available since: ES5

Syntax

array.lastIndexOf(searchElement, fromIndex)

Parameters

ParameterTypeDescription
searchElementanyThe value to find.
fromIndexnumberOptional. The position to search backwards from. From the end by default.

Return value

numberThe index of the last match, or -1.

Examples

The last occurrence:

const marks = [5, 3, 5, 4];
 
console.log(marks.lastIndexOf(5));
console.log(marks.indexOf(5));

Output:

2
0

Notes

Like indexOf(), it compares strictly and returns -1 when nothing is found.

See also

In the tutorial

  • Searching arrays — How to find an element in a JavaScript array: includes, indexOf, find, findIndex, some and every.

Thanks for reading!

Donate to the site — we work hard for you.