JavaScript .findLastIndex()

codewithfan's avatar
Published Jul 6, 2023
Contribute to Docs

The .findLastIndex() iterates through the array in reverse order and returns the index that passes the provided testing function. If no item passes, it returns -1.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

array.findLastIndex((element, index) => {...});

The function can be invoked with two arguments:

  • element: The current element in the iteration.
  • index (optional): The index of the array element.

Example

The following examples highlight the difference in how .findIndex() and .findLastIndex() iterate through an array:

let age = [13, 20, 15, 45, 1, 44, 80];
console.log('findIndex() method: ');
let findIndex = age.find((value, index) => {
console.log('Checking age index: ', index);
return value === 80;
});
findIndex() method:
Checking age index: 0
Checking age index: 1
Checking age index: 2
Checking age index: 3
Checking age index: 4
Checking age index: 5
Checking age index: 6
// Now .findLastIndex().
age = [13, 20, 15, 45, 1, 44, 80];
console.log('findLastIndex() method: ');
findIndex = age.findLastIndex((value, index) => {
console.log('Checking age index: ', index);
return value === 80;
});
findLastIndex() method:
Checking age index: 6

Note: If an element is known to be at the end of an array, using .findLastIndex can shorten the iteration path.

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours