New Array Prototype Methods
While reading The State of JS 2022, I came across two new Array.prototype
methods which I previously have not heard of. They are Array.prototype.at()
and Array.prototype.findLast()
.
.at()
This is going to be very useful for returning a value at a particular index in the array without having to iterate through. This great improve the time complexity to O(1) which is the best.
An example from MDN:
const array = [5, 12, 8, 130, 44];
array.at(2); // 8
The .at()
makes the code looks sweet, short and simple.
.findLast()
As we know, the .find()
method is one of the most commonly used method to-date. The .findLast()
is the opposite where it iterates the array in a reverse order. This is so useful if I want to search for a value that appear the last.
An example from MDN:
const array = [5, 12, 50, 130, 44];
array.findLast((item) => item > 45); // 130