JavaScript Array findLast()Method

Array 对象参考手册JavaScript Array Object

Example

Find the value of the last element with a value greater than 18:

const ages = [3, 10, 18, 20]; function checkAge(age) { return age > 18; } function myFunction() { document.getElementById("demo").innerHTML = ages.findLast(checkAge); }

Output:

2

Try it yourself »

Definition and Usage

The findLast() method returns the value of the last element that passes the condition test.

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

If no matching element is found, the findLast() method returns undefined.

The findLast() method does not execute the function for empty array elements.

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

Array find methods:

MethodDescription
indexOf()Find the index of the first element with a specified value
lastIndexOf()Find the index of the last element with a 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

findLast() is a feature of ES2023.

As of 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.findLast(function(currentValue, index, arr), thisValue)

Parameters

Parameter Description
function Required. A 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 where the current element is located.
thisValue Optional, defaults to undefined. A value passed as the function's this value.

Return Value

Type Description
Value The value of the last element that passes the condition test; if not found, returns undefined.

Find the value of the last element with a value greater than the specified number:

Example

<p><input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Try</button>
<p id="demo"></p>

<script>
const ages = [4, 12, 16, 20];

function checkAge(age) {
  return age > document.getElementById("ageToCheck").value;
}

function myFunction() {
  document.getElementById("demo").innerHTML = ages.findLast(checkAge);
}

Try it yourself »


Array 对象参考手册JavaScript Array Object

Other Extensions