Array find from last S4
- Stage: Stage 4
- Status: Finished
- ECMAScript edition: ES2023
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal adds findLast() and findLastIndex() methods to Array and TypedArray prototypes, allowing developers to find elements or their indices from the end of an array based on a condition. It addresses the need to iterate backward without mutation or complex index calculations, as required by workarounds like \[.arr\].reverse().find().
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
proposal-array-find-from-last
Proposal for .findLast() and .findLastIndex() methods on array and typed array.
Status
This is a stage 4 proposal.
Motivation
Finding an element in an array is a very common programming pattern.
The proposal has a major concerns: Semantical. Which means clearly representing the operation i want.
And with the changes. There's a sugar here: Performance. Avoid obvious overhead. And may improve the constant factors in the time complexity. Even there's not an order of magnitude change. But it's may useful in some performance-sensitive scenarios. eg: React render function.
ECMAScript currently supports {Array, %TypedArray%}.prototype.indexOf and {Array, %TypedArray%}.prototype.lastIndexOf to find an index of some value in the array.
There is also {Array, %TypedArray%}.prototype.find and {Array, %TypedArray%}.prototype.findIndex to find an element or its index in the array that satisfies a provided condition.
However, the language does not provide a method to find an element from the last to the first of an array with a condition function.
[...[]].reverse().find() is a workaround but there are two issues:
- unnecessary mutation (by reverse).
- unnecessary copy (to avoid mutation)
For .findIndex(), you are required to perform additional steps after calling the method (re-calculate the index and handle the -1) to calculate the result of [...arr].reverse().findIndex().
Therefore there is a third issue:
- complex index calculation
So, perhaps we need something directly and effectively. In this proposal, they are {Array, %TypedArray%}.prototype.findLast and {Array, %TypedArray%}.prototype.findLastIndex.
Scenarios
- You know find from last may have better performance (The target element on the tail of the array, could append with
pushorconcatin a queue or stack, eg: recently matched time point in a timeline). - You care about the order of the elements (May have duplicate item in the array, eg: last odd in the list of numbers).
- Etc.
Core features
Add {Array, %TypedArray%}.prototype.findLast and {Array, %TypedArray%}.prototype.findLastIndex.
This would behave the same as Array.prototype.find and Array.prototype.findIndex but would iterate from the last to the first.
eg:
Slides
- For stage 1: https://drive.google.com/file/d/1nzO9cjy4YlRa8h6ntTJ4Is8mx--sTuL_/view
- For stage 2: https://drive.google.com/file/d/1rhER8TZ5GsHDzl8nLvo8qSIQCUXPw3AQ/view
- For stage 3: https://kingwl.github.io/proposal-array-find-from-last-looking-for-stage-3-sides
- For stage 4: https://github.com/DanielRosenwasser/findLast-and-findLastIndex-for-Stage-4/blob/10cfbc10e155641e3260c68c4d5902ee28de116a/findLast%20%26%20findLastIndex%20for%20Stage%204%20(TC39%20June%202022).pdf
Polyfill
- core-js: you can find it in the ECMAScript proposals section
- es-shims: array.prototype.findlast / array.prototype.findlastindex
Related
- lodash.findLast
- lodash.findLastIndex
- ramda.findLast
- ramda.findLastIndex
- TypeScript internal findLast
- TypeScript internal findLastIndex
- @extra-array/find-right
Proposer
Champions:
- @Kingwl (Wenlu Wang, KWL)
- @DanielRosenwasser (Daniel Rosenwasser, DRR)