jsguides

Array.prototype.findLastIndex()

findLastIndex(callbackFn, thisArg)

findLastIndex() returns the index of the last element in an array that satisfies a testing function, or -1 if no element matches. It iterates from the end of the array toward the beginning.

Syntax

findLastIndex(callbackFn)
findLastIndex(callbackFn, thisArg)

Parameters

ParameterTypeDefaultDescription
callbackFnfunction-A function to execute for each element. Return true to signal a match.
thisArganyundefinedOptional. Value to use as this when executing callbackFn.

callbackFn Parameters

ParameterTypeDescription
elementanyThe current element being processed.
indexnumberThe index of the current element.
arrayarrayThe array that findLastIndex was called on.

Examples

Basic usage

const nums = [5, 12, 8, 130, 44];

const lastEvenIndex = nums.findLastIndex(n => n % 2 === 0);
console.log(lastEvenIndex);
// 4

Finding the last even number by value is straightforward, but the real power of findLastIndex() shows up when you need to locate an object by one of its properties rather than by the object itself. You supply a callback that inspects any field you care about, and the method scans from the end until it finds a match.

Finding the index of the last matching object

const users = [
  { id: 1, name: "Alice", active: true },
  { id: 2, name: "Bob", active: false },
  { id: 3, name: "Charlie", active: true },
  { id: 4, name: "Diana", active: true }
];

const lastActiveIndex = users.findLastIndex(u => u.active);
console.log(lastActiveIndex);
// 3

When multiple objects match, findLastIndex() returns the position of the one nearest to the end — typically the most recent entry. It stops iterating as soon as the callback returns truthy, so earlier elements are never visited. This makes it well-suited for append-only lists where the most interesting data is always at the tail.

When no match is found

const nums = [1, 3, 5, 7];

const index = nums.findLastIndex(n => n > 10);
console.log(index);
// -1

The -1 return value follows the same convention as indexOf() and findIndex(), so the check is familiar. Always verify the result before passing it to splice() or direct index access to avoid silently removing the last element of the array.

Common Patterns

Removing the last matching item

Combine findLastIndex with splice to remove the last matching element:

const items = ["a", "b", "c", "b", "d"];
const idx = items.findLastIndex(x => x === "b");

const removed = items.splice(idx, 1);
console.log(removed);
// ["b"]
console.log(items);
// ["a", "c", "b", "d"]

How findLastIndex() works

findLastIndex() iterates the array in reverse - starting at array.length - 1 and moving toward index 0 - calling the callback for each element. When the callback returns a truthy value, iteration stops immediately and that element’s index is returned. If the callback never returns truthy, -1 is returned. The callback receives the current element, its index, and the array.

Return value details

findLastIndex() returns -1 when no element satisfies the callback, mirroring findIndex(). Check for -1 before using the index in an operation like splice() or direct array access:

const idx = items.findLastIndex(x => x === 'target');
if (idx !== -1) {
  items.splice(idx, 1);
}

findLastIndex() vs lastIndexOf()

lastIndexOf(value) searches for a specific value using strict equality, scanning from the end. findLastIndex(fn) accepts a callback, enabling property-based matching or any complex condition. Use lastIndexOf for primitives where exact equality is sufficient; use findLastIndex when you need to match by a computed property or predicate.

const nums = [1, 2, 3, 2, 1];
nums.lastIndexOf(2);              // 3 - exact value
nums.findLastIndex(n => n < 3);   // 4 - condition-based

Sparse array behaviour

Like findLast(), findLastIndex() visits holes in sparse arrays and treats them as undefined. This differs from indexOf and lastIndexOf, which skip holes.

Choosing the right follow-up

The best follow-up to findLastIndex() is usually a mutation that needs the position, such as splice() or direct assignment. That is the main difference from findLast(), which gives you the value itself. If the position matters more than the element, findLastIndex() keeps the code honest because you do not need to search again just to act on the array. It is a small API choice, but it can remove one extra lookup from a mutation path.

Browser support

Introduced in ES2023. Available in Chrome 97+, Firefox 104+, Safari 15.4+, and Node.js 18+.

See Also