blog thumnail

Exploring the Future of JavaScript: Discovering the New findLast() and findLastIndex() Array Methods in the Proposed ECMAScript 2023 Specification

Tanveer Sayem / 2023-05-09

3 min read

JavaScript is a constantly evolving language with new features being proposed all the time. One of the latest proposals is for the addition of the .findLast() and .findLastIndex() methods on the array and typed array. This proposal has already reached Finished Proposals, which means that it is now supported by all modern Browsers and JavaScript Engines.
The motivation behind this proposal was to provide a way to find an element from the last to the first of an array with a condition function. Currently, JavaScript provides .indexOf(), .lastIndexOf(), .find(), and .findIndex() methods to find elements or indices in an array, but none of these methods provide a way to find an element from the last to the first of an array with a condition function.
To achieve this functionality before this proposal, one could use the .reverse() method to reverse the array and then use .find() or .findIndex() methods, but this approach has some issues like unnecessary mutation, unnecessary copy, and complex index calculation. Therefore, this proposal suggests adding .findLast() and .findLastIndex() methods to iterate from the last to the first of an array with a condition function, thus making it simpler and more efficient.
The proposed .findLast() and .findLastIndex() methods will behave the same as .find() and .findIndex(), respectively, but they will iterate from the last to the first of an array. This will provide a better semantic representation of the operation and potentially improve the performance in some scenarios.
For example, if the target element is on the tail of the array, it could be appended with push or concat in a queue or stack, and .findLast() and .findLastIndex() methods would be more efficient than their counterparts. Similarly, if you care about the order of the elements in the array, .findLast() and .findLastIndex() methods could be useful when there are duplicate items in the array.
Here is an example of how the proposed .findLast() and .findLastIndex() methods could be used:
const array = [{ value: 1 }, { value: 2 }, { value: 3 }, { value: 4 }];
// .find() and .findIndex()
array.find(n => n.value % 2 === 1); // { value: 1 }
array.findIndex(n => n.value % 2 === 1); // 0
// Before the proposal
[...array].reverse().find(n => n.value % 2 === 1); // { value: 3 }
array.length - 1 - [...array].reverse().findIndex(n => n.value % 2 === 1); // 2
array.length - 1 - [...array].reverse().findIndex(n => n.value === 42); // should be -1, but 4
// With the proposal
array.findLast(n => n.value % 2 === 1); // { value: 3 }
array.findLastIndex(n => n.value % 2 === 1); // 2
array.findLastIndex(n => n.value === 42); // -1
In summary, the .findLast() and .findLastIndex() methods are a useful addition to JavaScript's array and typed array methods. They provide a more semantic representation of the operation and potentially better performance in some scenarios.

Browser compatibility

thumbnail
thumbnail
If you click on my affiliates links and shop(anything, not just books), I am going to receive a tiny commission. AND… Most of the time, you will receive an offer. Win/Win! The products that I have are the ones I believe in.
amazon

Subscribe to the newsletter

Get emails from me about web development, tech, and early access to new articles.


  • Home
  • About
  • Newsletter
  • Twitter
  • Github
  • YouTube
  • Setup
  • Guestbook
  • Snippets