Array findLast() and findLastIndex()
The findLast() method of Array instances iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function. If no elements satisfy the testing function, undefined is returned.
If you need to find:
the first element that matches, use find().
the index of the last matching element in the array, use findLastIndex().
the index of a value, use indexOf(). (It's similar to findIndex(), but checks each element for equality with the value instead of using a testing function.)
whether a value exists in an array, use includes(). Again, it checks each element for equality with the value instead of using a testing function.
if any element satisfies the provided testing function, use some().
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 97 | 97 | 104 | 15.4 | 97 | 15.4 | |
| Built-in object | ||||||
| The findLastIndex() method of Array instances iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned. | 97 | 97 | 104 | 15.4 | 97 | 15.4 |
| The findLast() method of TypedArray instances iterates the typed array in reverse order and returns the value of the first element that satisfies the provided testing function. If no elements satisfy the testing function, undefined is returned. This method has the same algorithm as Array.prototype.findLast(). | 97 | 97 | 104 | 15.4 | 97 | 15.4 |
| The findLastIndex() method of TypedArray instances iterates the typed array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned. This method has the same algorithm as Array.prototype.findLastIndex(). | 97 | 97 | 104 | 15.4 | 97 | 15.4 |
Syntax
const nums = [1, 2, 3, 4, 5, 4, 3];
nums.findLast(n => n > 3); // 4 (the first occurrence from the end)
nums.findLastIndex(n => n > 3); // 5 Live demo
end from. position(index)
condition to combineelement that after from number. position to exists or read..
Use cases
-
Using Array findLast() and findLastIndex()
The findLast() method of Array instances iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.