Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Array findLastIndex() Method

JavaScript Array findLastIndex() Method

Array Object Reference JavaScript Array object

Example

Find the last element whose value is greater than 18:

const

ages

=

[

3

,

10

,

18

,

20

]

;

ages

.

findLastIndex

(

checkAge

)

;

function

checkAge

(

age

)

{

return

age

18

;

}

Output result:

Google,JYSHARE,Runoob,Taobao

Try it »


Definition and Usage

The findLastIndex() method executes a function for each element in the array.

The findLastIndex() method returns the index (position) of the last element that passes the test.

If no match is found, it returns -1.

For empty array elements, the findLastIndex() method does not execute the function.

The findLastIndex() method does not change the original array.

Array search methods:

Method Description
indexOf() Find the index of the first element with the specified value
lastIndexOf() Find the index of the last element with the specified value
find() Find the value of the first element that passes the test
findIndex() Find the index of the first element that passes the test
findLast() Find the value of the last element that passes the test
findLastIndex() Find the index of the last element that passes the test

Browser support

findLastIndex() is a feature of ES2023, and since July 2023 all modern browsers support this feature.

Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
Feb 2023 Feb 2023 Jul 2023 Mar 2023 May 2023

Syntax

array.findLastIndex(function(currentValue, index, arr), thisValue)

Parameters

Parameters Description
function Required. The function to run for each array element.
currentValue Required. The value of the current element.
index Optional. The index of the current element.
arr Optional. The array that the current element belongs to.
thisValue Optional, defaults to undefined. Passed as the this value of the function.

Return Value

Type Description
Numbe The index of the last element that passes the test; otherwise returns -1.

Array Object Reference JavaScript Array object