Table of contents

JavaScript Array.findLast() Method

The JavaScript Array.findLast() method is used to search an array and return the last element that satisfies a given condition (provided by a callback function). If no element meets the condition, it returns undefined. Unlike the find() method, which starts searching from the beginning of the array, the findLast() method searches the array from the end to the beginning.

For example, array.findLast(callback) is useful when you need to find the last matching element in an array based on certain criteria.

Syntax

The syntax for the Array.findLast() method is given below:

plaintext
array.findLast(callback(element, index, array), thisArg)

Parameters

The Array.findLast() method accepts two parameters:

  • callback (Required): A function that is executed on each element of the array, starting from the last element. It takes the following arguments:
    • element: The current element being processed.
    • index (Optional): The index of the current element.
    • array (Optional): The array being traversed.
  • thisArg (Optional): Value to use as this when executing the callback function. If not provided, undefined is used.

Return Value

The value of the last element that satisfies the condition. If no element matches the condition, it returns undefined.

Examples of JavaScript Array.findLast() Method

Example 1: Finding the Last Number Greater Than a Given Value

You can use the findLast() method to search for the last number in an array that meets a certain condition.

javascript
const numbers = [5, 12, 8, 130, 44];
const found = numbers.findLast(num => num > 10);
console.log(found);  // Output: 44

Explanation: The findLast() method returns the last element in the numbers array that is greater than 10. In this case, the method returns 44, which is the last number greater than 10.

Example 2: Finding the Last Object in an Array of Objects

You can use the findLast() method to locate the last object in an array based on a specific property.

javascript
const users = [
 { id: 1, name: 'Alice' },
 { id: 2, name: 'Bob' },
 { id: 3, name: 'Charlie' }
];
const user = users.findLast(user => user.id < 3);
console.log(user);  
// Output: { id: 2, name: 'Bob' }

Explanation: The findLast() method searches the users array from the end and returns the last object where the id property is less than 3. In this case, it returns the object { id: 2, name: 'Bob' }.

Example 3: Using findLast() with Index and Array Parameters

You can access both the index and array parameters in the callback function to create more complex searches.

javascript
const numbers = [4, 9, 16, 25];
const found = numbers.findLast((num, index) => num > 10 && index < 3);
console.log(found);  // Output: 16

Explanation: The findLast() method looks for the last element that is greater than 10 and whose index is less than 3. It returns 16, which is the last element that meets both conditions.

Example 4: Using findLast() with thisArg

You can provide a custom this context inside the callback function by using the thisArg parameter.

javascript
const threshold = {
 min: 20
};
const numbers = [5, 12, 8, 130, 44];
const found = numbers.findLast(function(num) {
 return num > this.min;
}, threshold);
console.log(found);  // Output: 130

Explanation: The thisArg parameter is used to set the this value to the threshold object. The findLast() method returns the last element greater than the min value of 20, which is 130.

Example 5: When No Match Is Found

If no elements satisfy the condition, the findLast() method returns undefined.

javascript
const numbers = [1, 2, 3, 4, 5];
const found = numbers.findLast(num => num > 10);
console.log(found);  // Output: undefined

Explanation: Since no elements in the numbers array are greater than 10, the findLast() method returns undefined.

Example 6: Using findLast() on an Empty Array

When the findLast() method is called on an empty array, it always returns undefined.

javascript
const emptyArray = [];
const found = emptyArray.findLast(num => num > 10);
console.log(found);  // Output: undefined

Explanation: Since the array is empty, there are no elements to test against the condition, and the method returns undefined.

JavaScript

Related Articles