JavaScript Array findLastIndex()
- Previous page findLast()
- Next page flat()
- Go back to the previous level JavaScript Array Reference Manual
Definition and Usage
findLastIndex()
The method executes a function for each array element.
findLastIndex()
The method returns the index (position) of the last element that passed the test.
If no matching element is found,findLastIndex()
The method returns -1.
findLastIndex()
The method does not execute the function for empty array elements.
findLastIndex()
The method does not change the original array.
Method | Search content |
---|---|
indexOf() | The index of the first element with the specified value. |
lastIndexOf() | The index of the last element with the specified value. |
find() | The value of the first element that passed the test. |
findIndex() | The index of the first element that passed the test. |
findLast() | The value of the last element that passed the test. |
findLastIndex() | The index of the last tested element. |
Instance
Example 1
Find the index of the last element with a value greater than 18:
const ages = [3, 10, 18, 20]; ages.findLastIndex(checkAge); function checkAge(age) { return age > 18; }
Example 2
Find the index of the last element with a value greater than the input value:
<p><input type="number" id="toCheck" value="18"></p> <button onclick="myFunction()">Test</button> <p>Any values above: <span id="demo"></span></p> <script> const numbers = [4, 12, 16, 20]; function checkValue(x) { return x > document.getElementById("toCheck").value; } function myFunction() { document.getElementById("demo").innerHTML = numbers.findLastIndex(checkValue); } </script>
Syntax
array.findLastIndex(function(currentValue, index, arr), thisValue)
Parameters
Parameters | Description |
---|---|
function() | Required. The function to be 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 to which the current element belongs. |
thisValue |
Optional. The value passed to the function. The default value is undefined. |
Return Value
Type | Description |
---|---|
Number |
The index of the last tested element. If not found, it returns -1. |
Browser Support
findLastIndex()
It is a feature of ES2023.
Since July 2023, all modern browsers support this method:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 110 | Edge 110 | Firefox 115 | Safari 16.4 | Opera 96 |
February 2023 | February 2023 | July 2023 | March 2023 | May 2023 |
- Previous page findLast()
- Next page flat()
- Go back to the previous level JavaScript Array Reference Manual