🚫 Stop Using array.length - 1. Here’s a Magical Alternative ✨

Hey there, dev fam πŸ‘‹

Have you ever found yourself accessing last element of an array again and again countless times like it is your full-time job like a typewriter.

Yeah... me too πŸ˜“πŸ˜“! I always ask myself, "Why? why? I mean, why do I have to do this same task over and over again?"

Honestly, it started feeling like torture -- kinda like an ex (not that I have had any, or current one either πŸ˜‚πŸ˜‚).

So, I went hunting for a cleaner way -- something built right into JavaScript that doesn't require any extra modification for day-to-day work.

And guess what? I found it it!, the magical method that cleaner, more efficient and most importantly, readable. In one word -- handy.


First let's see the way of finding the last element of array.

We'll use this array for all our examples:

let arr = [1, 2, 3, 4, 5, 6, 7];

1. Using length (The Old School Way):

This is how we usually get the last element:

const lastElement = arr[arr.length - 1];
console.log(lastElement); // 7

It is fin but it's repetitive and not readable. Have to manually calculate the last index every time.

2. Using .pop() (Compromising and Mutating Data):

If we don't need an array afterward .pop() can be helpful.

const lastElement = arr.pop();
console.log(lastElement); // 7

Yeah, this is not ideal, but in some use case it became useful.

⚠️ However, it mutates original array and is not helpful when need to access second last element or nth last element, because it is not so easy in that case and to compromise data.

3. Using slice() (Readable, but costly):

It is cleaner, and best part of it is negative indexing.

const lastElement = arr.slice(-1)[0];
console.log(lastElement); // 7

But the downside is, it create a new array instance, which can be wasteful in terms of memory optimization.

4. Best way .at() and my love πŸ₯°πŸ’–πŸ”₯:

Finally, the hero of the post: .at().
It is simple, clean, performant. It supports negative indexing, and I call it my dream method -- like dream wife of every man.

It is my favorite and I call it the magical method.

Here are some examples:

Positive Indexing:

console.log(arr.at(0)); // 1
console.log(arr.at(1)); // 2
console.log(arr.at(2)); // 3

Negative Indexing:

console.log(arr.at(-1)); // 7
console.log(arr.at(-2)); // 6
console.log(arr.at(-3)); // 5

Using negative index we are accessing last element or second last element just without any extra calculation like math calculation in mind arr.length - 1 or arr.length - n.

Let's see what if we use traditional way:

console.log(arr[arr.length - 1]); // 7
console.log(arr[arr.length - 2]); // 6
console.log(arr[arr.length - 3]); // 5

See the difference?

Yeah, from above comparison of code, you may feel why I was amused by that .at method -- because of its

  • simplicity,

  • clarity and

  • readability.

πŸ€” Why .at() Is Awesome

  • Supports negative indices

  • Doesn’t mutate the array

  • Works on strings too!

  • Makes your code cleaner and easier to read

It’s not that arr.length - 1 was terrible, but .at() feels more modern and clean.


I’ll be sharing more tricks in the future. ✨

Share your tricks or technique that make your like easier. Drop it in comments πŸ‘‡πŸ‘‡.

Thanks for reading it.

Here's my LinkedIn profile

Here's my GitHub profile

Thank you

0
Subscribe to my newsletter

Read articles from Shakil Abdus Shohid directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shakil Abdus Shohid
Shakil Abdus Shohid