Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Array.prototype.findLastIndex

Returns the index of the last element that passes a test.

Available since: ES2023

Syntax

array.findLastIndex(callbackFn)

Parameters

ParameterTypeDescription
callbackFnFunctionA 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
-1

Notes

Indexes are counted from the start of the array even though the search goes from the end.

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.