Array.prototype.findLastIndex()

Returns the index of the last element passing the test, or -1 if none does.

Signature
findLastIndex(callbackFn)
findLastIndex(callbackFn, thisArg)
  • Added in JavaScript ES2023

Parameters

ParameterTypeDescription
$callbackFnFunctionFunction run for each element from the end backwards, called with (element, index, array). Return a truthy value to signal a match.
$thisArgoptionalany= undefinedValue to use as this when executing callbackFn. Ignored by arrow functions.

Return value

numberThe index of the last (highest-index) element for which callbackFn returned a truthy value, or -1 if no element matched.

Errors

Nothing of its own — errors thrown by callbackFn propagate and stop the search.

findLastIndex() walks from length - 1 down to 0 and returns the index of the first element whose callback result is truthy — the last match in array order. No match gives -1.

When to reach for it

Use it when you need the position of the last match rather than the value: trimming trailing blanks, slicing everything after the final delimiter, splicing out the most recent duplicate, or finding where a run of good data ends. lastIndexOf() covers the same ground when you are comparing a primitive with ===; findLastIndex() is the version that takes a predicate, so it handles object fields, compound conditions and NaN.

What trips people up

The -1 return needs the same care as in findIndex(). It is a valid argument to at() and slice(), so an unguarded miss silently addresses the end of the array. Note the useful special case: arr.slice(0, idx + 1) does the right thing on a miss, returning an empty array, because -1 + 1 is 0.

Holes in sparse arrays are visited and treated as undefined, so a predicate testing for undefined will match them — unlike lastIndexOf(), which skips holes entirely.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Trim trailing empty cells from a spreadsheet row
  • Find where the last failing test appears in a log
  • Locate the final separator to split a path or filename
  • Remove the most recent duplicate entry from a history list

Frequently asked questions

What is the difference between findLastIndex() and lastIndexOf()?

lastIndexOf() compares a value with strict equality; findLastIndex() runs a predicate you supply. Use lastIndexOf() for primitives and findLastIndex() for object properties, compound conditions, or matching NaN.

What does findLastIndex() return when nothing matches?

-1. Check with !== -1 before indexing, since -1 is a legal argument to at() and slice() and will quietly point at the end of the array.

Can I use findLastIndex() in older browsers?

Only via a polyfill — it is ES2023, available in major browsers since August 2022 and in Node.js from v18. Before that, the fallback was arr.length - 1 - [...arr].reverse().findIndex(fn), with a separate -1 check.

More examples

Trimming trailing blanks

The first slice prints ['a', 'b']. The second prints [] — the miss returns -1, and slice(0, 0) is empty, so the + 1 idiom degrades correctly without an explicit guard. This is the one place -1 works in your favour.

Example · javascript
const cells = ["a", "b", "", "", ""];

const end = cells.findLastIndex((c) => c !== "");
console.log(cells.slice(0, end + 1));

const allBlank = ["", ""];
console.log(allBlank.slice(0, allBlank.findLastIndex((c) => c !== "") + 1));

Holes are visited, unlike lastIndexOf()

findLastIndex prints 1 and lastIndexOf prints -1. The find-family methods treat an empty slot as an undefined element and visit it; the indexOf family skips holes altogether. Same array, opposite answers.

Example · javascript
const sparse = [1, , 3];

console.log(sparse.findLastIndex((x) => x === undefined));
console.log(sparse.lastIndexOf(undefined));

Discussion

  • Be the first to comment on this lesson.