Array. prototype. findLastIndex()
Returns the index of the last element matching a condition, searching from the end, or -1.
- Returns
- The index of the last matching element, or
-1. - Changes the original
- No
Example
#const lessons = [
{ title: 'Variables', done: true },
{ title: 'Loops', done: true },
{ title: 'Functions', done: false },
{ title: 'Arrays', done: false }
]
console.log(lessons.findLastIndex((lesson) => lesson.done))
console.log(lessons.findIndex((lesson) => !lesson.done))
console.log(lessons.findLastIndex((lesson) => lesson.title === 'Classes'))Definition and usage
#The findLastIndex() method checks elements with a testing function, starting from the end of the array. It returns the index of the first match it finds, which is the last matching element in the array’s order. When no element meets the condition, the result is -1.
It’s the backward-searching counterpart to findIndex(). It’s handy for chronological data when you care about the most recent entry: the last completed lesson, the last failed login attempt, or the last message from a mentor.
The index lets you not just read the element, but also replace it, remove it with splice(), or cut out a chunk of the array with slice(). If you only need the element itself, findLast() is simpler.
Syntax
#array.findLastIndex(callbackFn)
array.findLastIndex(callbackFn, thisArg)Parameters
#callbackFn
function (element, index, array)
The testing function, called from the last element toward the first.thisArg
optional
Thethisvalue inside the function.
More examples
#const log = ['ok', 'error', 'ok', 'error', 'ok', 'ok']
const lastError = log.findLastIndex((entry) => entry === 'error')
console.log(lastError)
console.log(log.slice(lastError + 1))When the log has no error at all, findLastIndex() returns -1, and slice(0) returns the whole array. Here, that happens to be the result we want.
const attempts = [
{ id: 1, score: 40 },
{ id: 2, score: 85 },
{ id: 3, score: 55 }
]
const index = attempts.findLastIndex((attempt) => attempt.score < 60)
if (index !== -1) {
attempts[index] = { ...attempts[index], retry: true }
}
console.log(index)
console.log(attempts[index])Browser support
#Widely available · since 2022
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Check for
-1before using the index. Assigning toarray[-1]won’t change any element; it just adds a strange property named "-1" to the array. - For simple values,
lastIndexOf()is enough.findLastIndex()is needed when you’re searching by a condition, such as an object’s field.
Related entries
#- Array.prototype.findLast()Returns the last element that matches a condition, searching the array from the end.
- Array.prototype.findIndex()Returns the index of the first element that matches a condition, or -1.
- Array.prototype.lastIndexOf()Returns the index of the last occurrence of a value in the array, or -1 when it isn’t there.
- Array.prototype.splice()Removes, replaces, or inserts elements in place, changing the array.
Spotted a mistake or missing an example? Write to us.