Array.prototype.findLast()
Returns the last element that satisfies the test, searching from the end backwards.
findLast(callbackFn)
findLast(callbackFn, thisArg)- Added in JavaScript ES2023
Parameters
| Parameter | Type | Description |
|---|---|---|
$callbackFn | Function | Function run for each element from the end backwards, called with (element, index, array). Return a truthy value to signal a match. |
$thisArgoptional | any= undefined | Value to use as this when executing callbackFn. Ignored by arrow functions. |
Return value
any | undefinedThe last (highest-index) element for which callbackFn returned a truthy value, or undefined if no element matched.
Errors
Nothing of its own — errors thrown by callbackFn propagate and stop the search.
findLast() is find() run backwards. It starts at length - 1, walks toward index 0, and returns the first element whose callback result is truthy — which is the last such element in array order. A miss returns undefined.
When to reach for it
Chronological data is the obvious case. Logs, event streams, revisions and time series are usually appended, so "the most recent X" means the last matching element. Before ES2023 people wrote [...arr].reverse().find(fn), which copies the whole array and inverts the indexes reported to the callback. findLast() does neither: no allocation, and the index argument is still the real index in the original array.
What trips people up
Like find(), a returned undefined is ambiguous — it might be a genuine undefined element. Use findLastIndex() and check for -1 when that matters.
Also note that reverse() mutates in place, so the old workaround damaged the caller's array unless it was copied first. If you are migrating that pattern, findLast() replaces the whole expression, not just the find() part.
Example
When to use it
- Get the most recent successful entry from an append-only log
- Find the latest revision of a document that passed review
- Locate the last checkpoint before a failure in an event list
- Pick the final row in a table that still has data
Frequently asked questions
What is the difference between find() and findLast()?
find() scans from index 0 forward and returns the first match; findLast() scans from the last index backward and returns the last match. On an array with exactly one match they return the same element.
Is findLast() the same as reverse().find()?
It gives the same element but is safer and faster: reverse() mutates the original array, a copy costs an allocation, and both report reversed indexes to the callback. findLast() avoids all three.
Which browsers and Node versions support findLast()?
It is part of ES2023 and has been available across major browsers since August 2022; Node.js has shipped it since v18. Older runtimes need a polyfill or the copy-and-reverse fallback.
More examples
Compared with the old reverse() workaround
All three logs agree on the value 8, and nums is still [1, 8, 3, 8, 2] because toReversed() copies rather than mutating. The last line prints 'index 3' first, showing that the callback receives the true index 3 rather than a reversed 1.
const nums = [1, 8, 3, 8, 2];
console.log(nums.findLast((n) => n === 8));
console.log(nums.toReversed().find((n) => n === 8));
console.log(nums);
console.log(nums.findLast((n, i) => n === 8 && console.log("index", i) !== 1));Scanning an event log
Prints 'warn' then undefined. The first call stops at index 3 — the later warn — rather than the error at index 2, because it is scanning from the end. The miss returns undefined, so guard it before reading properties.
const events = ["boot", "warn", "error", "warn", "idle"];
console.log(events.findLast((e) => e === "error" || e === "warn"));
console.log(events.findLast((e) => e === "fatal"));
Discussion