Member-only story
Most unused JavaScript Array functions
You probably won’t use them every day, but let’s explore these few lesser-known JavaScript array functions
You can read this article free by clicking here.
As a front-end developer, JavaScript is my primary programming language. However, I often find myself using the same built-in functions or relying on substitutes, missing out on the powerful features that the JavaScript engine already provides. I wanted to share some of the most underused JavaScript array functions along with their use cases, which might make you realize how they could have helped you write cleaner, more efficient code in terms of time complexity, space complexity, usability, and readability.
So let’s begin!
How to get the last element of the array?
let arr = [11,24,45,26,23,16,71,92,0];
// Option 1
let last = arr[arr.length - 1];
// Option 2
let last = arr.at(-1);
// You can even do it
let SecondLast = arr.at(-2);
The .at(-1)
the method is more readable and avoids directly calculating, making it easier to work with the last element (or other relative positions) in the array. The .at()
method can handle negative indices in a more intuitive way, especially when accessing elements…