Array.prototype.findLastIndex
Returns the index of the last element that passes a test.
Available since: ES2023
Syntax
array.findLastIndex(callbackFn)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | Function | A test function: receives the element, its index and the array, and returns true for a match. |
Return value
numberThe index of the last matching element, or -1.
Examples
The index of the last number above 10:
console.log([12, 5, 30, 8].findLastIndex((n) => n > 10));
console.log([1, 2].findLastIndex((n) => n > 10));Output:
2
-1Notes
Indexes are counted from the start of the array even though the search goes from the end.
See also
- Array.prototype.findIndex — Returns the index of the first element that passes a test.
- Array.prototype.findLast — Searches from the end of an array for the first element that passes a test.
- Array.prototype.lastIndexOf — Finds a value from the end of an array and returns its index.
In the tutorial
- Searching arrays — How to find an element in a JavaScript array: includes, indexOf, find, findIndex, some and every.