The findLast method in JavaScript is a useful tool that allows you to find the last element in an array that meets a specific condition. It works similarly to the find() method, but starts searching from the end of the array instead of the beginning. In this post, we’ll explore how to use the findLast() method with a basic example to help you understand it better.

Syntax of findLast()

The findLast() method takes a callback function as its argument and applies it to each element of the array, starting from the end and moving towards the beginning. It returns the first element that matches the condition or undefined if no element is found.

array.findLast(callback(element, index, array), thisArg)
  • callback: A function executed on every element of the array. It should return true when the desired element is found.
  • element: The current element being processed.
  • index (optional): The index of the current element.
  • array (optional): The array findLast() was called upon.
  • thisArg (optional): Value to use as this when executing the callback.

Basic Example: Finding the Last Even Number

Let’s start with a simple example where we use findLast() to find the last even number in an array.

const numbers = [1, 3, 5, 8, 10, 15];

const lastEven = numbers.findLast(function(num) {
    return num % 2 === 0;
});

console.log(lastEven); // Output: 10

Explanation:

  • Original array: We have an array of numbers [1, 3, 5, 8, 10, 15].
  • Callback function: The callback checks if a number is even using num % 2 === 0. It starts checking from the end of the array.
  • Result: The findLast() method returns the value 10, which is the last even number in the array.

Simplified Example Using Arrow Functions

Let’s rewrite the example using an arrow function to make the code cleaner and more concise.

const numbers = [1, 3, 5, 8, 10, 15];

const lastEven = numbers.findLast(num => num % 2 === 0);

console.log(lastEven); // Output: 10

Explanation:

  • Arrow function: We use num => num % 2 === 0 to simplify the code, which still returns the last even number 10.

Example 2: Finding the Last Object that Meets a Condition

The findLast() method is especially useful when working with arrays of objects. Suppose we have a list of people, and we want to find the last person who is older than 18.

const people = [
  { name: 'Alice', age: 16 },
  { name: 'Bob', age: 25 },
  { name: 'Charlie', age: 17 },
  { name: 'David', age: 30 }
];

const lastAdult = people.findLast(person => person.age > 18);

console.log(lastAdult); 
// Output: { name: 'David', age: 30 }

Explanation:

  • Original array: We have an array of objects, where each object represents a person with a name and age.
  • Callback function: The function person => person.age > 18 checks if the person’s age is greater than 18.
  • Result: The findLast() method returns the last person who is an adult, which is { name: 'David', age: 30 }.

Conclusion

The findLast() method is a convenient way to locate the last element in an array that meets a specific condition. Here’s a quick recap:

  • Starts from the End: Unlike find(), findLast() begins searching from the end of the array.
  • Use Cases: It’s useful when you need to find the most recent match in an array.
  • Returns undefined if No Match: If no element matches the condition, it returns undefined.

With this simple guide, you can start using findLast() to efficiently find the elements you need, starting from the end of your array.

Happy coding!

Subscribe to Youtube Channel

Leave a Reply

Your email address will not be published. Required fields are marked *