search
Array.prototype.findLast
Walks the array from the END and returns the last element for which the callback returns truthy (ES2023). Reach for it when the most recent matching entry is what you want.
arr.findLast(callbackFn[, thisArg]) Parameters
| Parameter | Purpose |
|---|---|
| callbackFn(element, index, array) | Predicate applied in reverse order |
| thisArg | Value used as `this` inside callbackFn |
Examples
console.log([1,2,3,4].findLast(n => n < 4)); Logs: 3
const events = [{t:1,ok:true},{t:2,ok:false},{t:3,ok:true}]; console.log(events.findLast(e => e.ok)); Logs: {t: 3, ok: true}
console.log([1,2,3].findLast(n => n > 99)); Logs: undefined
console.log([1,2,3,4,5].findLast(n => n % 2 === 0)); Logs: 4
Gotcha
Requires ES2023 (Node 18+, modern browsers 2022+). Equivalent to a manual reverse-loop but does not mutate the array.
Related methods
Array.prototype.find
Returns the first element for which the callback returns truthy, or undefined if none match. Reach for it when you need the matching item itself, not just its index.
Array.prototype.findIndex
Returns the index of the first element for which the callback returns truthy, or -1 if none match. Reach for it when you need the position — e.g. to splice or replace.
Array.prototype.reverse
Reverses the order of elements in place and returns the same (mutated) array. Reach for it when you truly want to reverse the source; otherwise use toReversed().
Array.prototype.indexOf
Returns the first index where searchElement is strictly equal (===) to an array item, or -1. Reach for it for a fast, exact primitive lookup — but not for objects or NaN.
Array.prototype.includes
Returns true if the array contains searchElement using SameValueZero equality (ES2016). Reach for it as the readable boolean alternative to `indexOf(x) !== -1`.