115 of 264 menu

findLastIndex method

The findLastIndex method helps to find the index of the first element from the end of an array that matches the condition according to the callback parameter. If there is no element, then -1 is returned.

Syntax

array.findLastIndex(function);

Example

Let's find the index of a positive number in an array:

let arr = [-12, -13, 14, 15]; let res = arr.findLastIndex(function(elem) { return elem > 0; }); console.log(res);

The code execution result:

3

Example

And now let's find the index of an element that is not in an array:

let arr = ['a', 'b', 'c', 'd']; let res = arr.findLastIndex(function(elem) { return elem === 'f'; }); console.log(res);

The code execution result:

-1

See also

  • the find method
    that searches for an element in an array
  • the findIndex method
    that searches for the index of an element in an array
  • the findLast method
    that searches for an element from the end of an array
byenru