Skip to content

Array findLast() and findLastIndex(): search from the end

  • What findLast() and findLastIndex() return and how they mirror find() and findIndex().
  • Why the old reverse() workarounds mutate data or return wrong indexes.
  • How to query an append-only event log with them.
  • What TypeScript and older runtimes need.

TL;DR: array.findLast(predicate) returns the last element for which predicate returns a truthy value, or undefined. array.findLastIndex(predicate) returns that element’s index (counted from the start, as usual), or -1. They search from the end and stop at the first match they meet.

They were added in ECMAScript 2023 and are supported by all current browsers and Node.js 18 and later. They behave exactly like find() and findIndex() except for direction; this page covers only what the direction changes.

Data that grows by appending, such as logs, histories and timelines, keeps the newest entries at the end. “The most recent X” is a search from the end, and before ES2023 there was no built-in for it with a condition. lastIndexOf() only compares values with ===.

The usual workarounds each have a defect. reverse() reverses the array in place, so you must copy first. Translating an index found in a reversed copy back to the original is easy to get wrong when nothing matches. This sample uses the status history of one order in the invented OrdersPortal app.

Running the samples: save a sample as a .mjs file and run it with Node.js 22 (node sample.mjs; Node.js 18 or later works), or paste it into a browser console. The outputs shown were produced with Node.js 22.23.

// OrdersPortal: an order's status history, oldest first (append-only).
const history = [
{ status: 'created', at: '2026-11-02T09:00Z' },
{ status: 'payment_failed', at: '2026-11-02T09:01Z' },
{ status: 'paid', at: '2026-11-02T09:05Z' },
{ status: 'shipped', at: '2026-11-03T14:20Z' },
{ status: 'payment_failed', at: '2026-11-04T08:00Z' }, // a later top-up charge failed
];
// Before ES2023, option 1: reverse() MUTATES the array.
const copy = [...history];
console.log(copy.reverse().find((e) => e.status === 'payment_failed').at); // 2026-11-04T08:00Z
// Option 2: copy, reverse, then translate the index, and remember to handle -1.
const reversedIndex = [...history].reverse().findIndex((e) => e.status === 'refunded');
console.log(history.length - 1 - reversedIndex); // 5 <- wrong: should be -1
// After: search from the end, no copy, real indexes.
console.log(history.findLast((e) => e.status === 'payment_failed').at); // 2026-11-04T08:00Z
console.log(history.findLastIndex((e) => e.status === 'payment_failed')); // 4
console.log(history.findLastIndex((e) => e.status === 'refunded')); // -1
console.log(history.findLast((e) => e.status === 'refunded')); // undefined

length - 1 - (-1) equals length, an index that does not exist, which is how the “not found” case quietly turns into a bogus position.

// Fragment: signatures
array.findLast(predicate, thisArg?) // element or undefined
array.findLastIndex(predicate, thisArg?) // index or -1
// predicate(value, index, array) -> truthy to stop

In the specification, findLast and findLastIndex call the same FindViaPredicate operation as find and findIndex, with the direction set to descending. So the rules are shared: the predicate receives (value, index, array); the index range is fixed before the first call; holes are visited and read as undefined; a non-callable predicate throws a TypeError; and the methods are generic, so they work on array-likes and exist on typed arrays too.

// Descending order: the predicate sees the last index first.
const seen = [];
['a', 'b', 'c'].findLast((v, i) => { seen.push(i); return v === 'b'; });
console.log(seen); // [ 2, 1 ]
// Holes are visited as undefined, like find.
console.log([1, , 3].findLastIndex((v) => v === undefined)); // 1
// lastIndexOf compares values with ===, so it cannot take a condition or find NaN.
const readings = [3, NaN, 7, NaN];
console.log(readings.lastIndexOf(NaN)); // -1
console.log(readings.findLastIndex((n) => Number.isNaN(n))); // 3
// Typed arrays have the same methods.
console.log(new Int16Array([5, -2, 8, -9]).findLast((n) => n < 0)); // -9

The returned index is a normal index from the start of the array, so you can pass it straight to slice(), with() or splice().

OrdersPortal support staff ask two questions about an order: “what was its status on a given date?” and “what happened after the last successful payment?”. The event log is append-only and sorted by time, so both are searches from the end.

// order-timeline.mjs - answer support questions from an append-only event log.
const events = [
{ type: 'status', value: 'created', at: Date.parse('2026-11-02T09:00Z') },
{ type: 'payment', value: 'failed', at: Date.parse('2026-11-02T09:01Z') },
{ type: 'payment', value: 'succeeded', at: Date.parse('2026-11-02T09:05Z') },
{ type: 'status', value: 'paid', at: Date.parse('2026-11-02T09:05Z') },
{ type: 'status', value: 'shipped', at: Date.parse('2026-11-03T14:20Z') },
{ type: 'status', value: 'delivered', at: Date.parse('2026-11-05T11:42Z') },
];
// "What was the status at a given moment?" The newest status event at or before it.
export function statusAt(log, isoTime) {
const t = Date.parse(isoTime);
return log.findLast((e) => e.type === 'status' && e.at <= t)?.value ?? 'unknown';
}
// "Everything after the last successful payment" (for a refund audit).
export function eventsSinceLastPayment(log) {
const i = log.findLastIndex((e) => e.type === 'payment' && e.value === 'succeeded');
return i === -1 ? log : log.slice(i + 1);
}
console.log(statusAt(events, '2026-11-04T00:00Z')); // shipped
console.log(statusAt(events, '2026-11-01T00:00Z')); // unknown
console.log(eventsSinceLastPayment(events).map((e) => e.value)); // [ 'paid', 'shipped', 'delivered' ]

findLast() is a linear scan. For an order with a dozen events that is ideal. For a sorted log with millions of entries, a binary search on at finds the position in O(log n).

  • Assuming the index counts from the end. findLastIndex() returns a normal index from the start; 4 means array[4], not “four from the end”.
  • Keeping the reverse() workaround. array.reverse().find(...) reorders the caller’s data in place. If you must support runtimes without findLast(), copy first with [...array].reverse() or use toReversed(), or polyfill.
  • Treating findLastIndex() as a boolean. 0 is falsy and -1 is truthy; compare with -1.
  • Assuming “last” means “newest”. It means the highest index. If the array is not kept in time order, sort it or search by timestamp.
  • Expecting lastIndexOf() to behave the same. It takes a value, not a predicate, uses === (so it never finds NaN) and skips holes.
You want Use
The last element matching a condition findLast()
Its index findLastIndex()
The last occurrence of a known value lastIndexOf()
The first match find() / findIndex()
Just the last element, no condition at(-1)
Every match, newest first filter(), then toReversed()

According to MDN’s browser compatibility data, findLast() and findLastIndex() are supported in Chrome 97, Edge 97, Firefox 104, Safari 15.4 (including iOS), Node.js 18.0, Deno 1.16 and Bun 1.0. Typed-array versions have the same minimum versions. Internet Explorer never supported them.

TypeScript: the types are in lib.es2023.array.d.ts. With an older lib or target, TypeScript 6.0.3 reports Property 'findLast' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later. Add es2023 (or later) to lib; TypeScript does not polyfill the runtime method.

Polyfill: core-js and the es-shims packages array.prototype.findlast and array.prototype.findlastindex cover older runtimes.

1. What does [1, 2, 3].findLastIndex((n) => n > 5) return, and why does the old workaround get this wrong? It returns -1. The workaround arr.length - 1 - [...arr].reverse().findIndex(fn) computes 3 - 1 - (-1), which is 3, a position that does not exist, unless you special-case -1.

2. How do findLast() and lastIndexOf() differ? findLast() takes a predicate and returns an element; lastIndexOf() takes a value, compares it with === and returns an index. lastIndexOf() skips holes and cannot find NaN; findLastIndex() with Number.isNaN can.

3. Senior scenario: a chat feature shows “last read message” markers by calling messages.findLast(...) on every render, and conversations can hold 100,000 messages. Is that a problem, and what would you do? Each call scans from the end, so it is cheap when the match is near the end and O(n) when it is not, on every render. Because messages are sorted by time, store the last-read message ID or timestamp and locate it with a binary search, or keep the index and update it incrementally as messages arrive. Use findLast() for small or unsorted arrays, where its clarity matters more than the scan.

  • findLast() and findLastIndex() (ES2023) are find() and findIndex() searching from the end.
  • They replace reverse() workarounds that mutate the array or miscompute “not found”.
  • The returned index counts from the start; “not found” is undefined or -1.
  • Supported from Node.js 18 and 2022-era browsers; TypeScript needs lib es2023.