JavaScript Reference ยท Array
JavaScript Array findLast() method
Reference for JavaScript Array findLast(): return the last element that passes a test by searching from the end, with edge cases, examples and support notes.
The findLast() method works like find(), but it searches from the end of the array toward the start. It returns the last element for which your callback returns a truthy value, or undefined if none passes.
It is handy for logs, histories and event lists, where the newest entry that matches a condition is the one you want, and it saves you from reversing a copy of the array first.
[1, 4, 6, 3].findLast((n) => n % 2 === 0); // 6
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
findLast(callbackFn) findLast(callbackFn, thisArg)
| Name | Description |
|---|---|
callbackFn |
Called as callbackFn(element, index, array). Return a truthy value for the element you want. |
thisArg |
Optional. Value used as this inside callbackFn (ignored by arrow functions). |
Return value: the last element that passed, or undefined if none did.
Exceptions: a TypeError if callbackFn is not a function.
Usage notes
- Reverse visiting order. The callback sees indexes from
length - 1down to0and stops at the first pass, which is the last match in array order. Theindexargument is still the real index. - Same
undefinedambiguity asfind(). If the array can containundefined, usefindLastIndex()and compare the result with-1. - No copy. It returns the element itself, so objects are shared with the array.
- Holes are visited as
undefined, likefind(). - Instead of reverse:
arr.toReversed().find(fn)or[...arr].reverse().find(fn)does the same job but allocates a copy. - Plain values: for a value (not a condition),
lastIndexOf()does the equivalent search. - Ties go to the later element: when several elements pass the test, you get the one closest to the end, which matters when they are different objects.
Browser support
findLast() is newer than find(). It is available in current browsers, but older ones and some older Node versions lack it, so check a compatibility table if you support them.