DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Tips: How to get last element of an array in javascipt
Sakib Ahmed
Sakib Ahmed

Posted on

Tips: How to get last element of an array in javascipt

An array is a data structure, which can store a fixed-size collection of elements of the same data type

If you have worked with JavaScript arrays before, you might know that they can be destructured much like objects. This is most commonly used to extract the first value of an array or the values of an array with a known length.

But destructuring can go much further, as it allows you to extract the length property of an array. Add this to the fact that extracted variables can be used in the destructuring assignment itself and you can put together a one-liner to extract the last element of an array.

const arr = [1, 2, 3];
const { 0: first, length, [length - 1]: last } = arr;
first; // 1
last; // 3
length; // 3
Enter fullscreen mode Exit fullscreen mode

While this technique is interesting, it has a couple of caveats. First off, you have to extract the length property, which creates an additional variable for it. And secondly, it doesn’t have any significant performance advantages over other options, such as using Array.prototype.slice().

Another way we can do is, first we need array length then subtract 1 to get the last element index.

Take a loot at code

const arr = [1, 2, 3];
const lastEle = arr[arr.length - 1]
lastEle; // 3
Enter fullscreen mode Exit fullscreen mode

arr.length = 3
3 – 1 = 2
arr[2] = 3

website DevvSakib.Me

Top comments (12)

Collapse
miketalbot profile image
Mike Talbot
   const arr = [1, 2, 3]
   const lastElement = arr.at(-1)
Enter fullscreen mode Exit fullscreen mode
Collapse
jfbrennan profile image
Jordan Brennan

Booo. I will die angry that last, lastItem(), etc. have all been rejected by the powers that be.

Collapse
muhammed_navas profile image
MUHAMMED NAVAS

it is making an error, arr.at is not a function.

Collapse
miketalbot profile image
Mike Talbot • Edited on

This is modern ES6 syntax, you will either need a modern browser/version of node or to use a transpiler like Babel.

This is the currently supported list:

caniuse.com/mdn-javascript_builtin...

Collapse
mrcaidev profile image
Yuwang Cai

I especially love this feature when combined with optional chain, in TypeScript, e.g. unknownArr?.at(1) ?? 0. It makes the code so much cleaner.

Collapse
quocbahuynh profile image
quocbahuynh

perfect

Collapse
jonrandy profile image
Jon Randy

Beat me to it

Collapse
codemeop profile image
codemeop

i always use arr.length-1

Collapse
ludovic974 profile image
Ludovic974

arr.pop() ?????

Collapse
devvsakib profile image
Sakib Ahmed Author

pop() method removes the last element from an array and returns that element. This method changes the length of the array.

Collapse
schemetastic profile image
Rodrigo Isaias Calix • Edited on

Cool, i knew a little about the pop() method but now is more clear to me.

Here is a small example of this

var fruits = ['apples', 'oranges', 'grapes'];
var myLastFruit = fruits.pop();
console.log(fruits, myLastFruit);
Enter fullscreen mode Exit fullscreen mode

This would log:

Array [ "apples", "oranges" ] grapes
Enter fullscreen mode Exit fullscreen mode

As you can see 'grapes' now doesn't form part of the fruits variable but at the same time the pop() method returns the last item of the array. So clearly, if someone is going to use this method must be aware of this.

Collapse
yxsh profile image
Yash

well i always do

array[array.length - 1]
Enter fullscreen mode Exit fullscreen mode

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.