DEV Community

Cover image for Get last element of array in JavsScript

Get last element of array in JavsScript

vickyktk on January 18, 2022

Whatever the programming language you code in ,I am sure you will be playing with arrays every day. So today we will be looking at options we can g...
Collapse
 
giokhar profile image
Giorgi Kharshiladze

I think you are missing one of the methods that was added not too long ago.
Array.prototype.at(). To get the last element in an array you can do myArr.at(-1).
developer.mozilla.org/en-US/docs/W...

Collapse
 
vickyktk profile image
vickyktk

Oh Yes !!! Thank You. This pretty usefull and easy

Collapse
 
dagropp profile image
Daniel Gropp

While it’s a cool new method, keep in mind that currently it’s not supported in Safari based devices.

Collapse
 
lexlohr profile image
Alex Lohr

You can easily polyfill it:

if (![].at) {
  Array.prototype.at = function(pos) { return this.slice(pos, pos + 1)[0] }
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
nbouvrette profile image
Nicolas Bouvrette

This won't work with negative values.

Collapse
 
labspl profile image
Wojciech

In Safari Technology Preview you can activate it via Develop > Experimental Features

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

A somewhat unusual way:

const str = 'Hello'
const {length, [length-1]: last} = str

console.log(last) // 'o'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
labspl profile image
Wojciech

Using console.log in prod is discouraged

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Naked flames in firework factories are discouraged too

Thread Thread
 
labspl profile image
Wojciech

Laugh all you want, but console.log can be abused by some threat actors very easily. I mean, output of console.log is not escaped ( or otherwise secured ) in any way

Thread Thread
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

I'm not laughing, but your comment was entirely irrelevant. The console.log I used was simply used to display the result of some sample code - in a similar way to the way the original poster used it. No-one is suggesting in any way that it should be used in production code.

Thread Thread
 
labspl profile image
Wojciech • Edited

OK. So imagine this:

How are web-enabled systems attacked? Of course there are many ways, but first and the most used one method of probing is through reverse engineering output of console.log.

Thread Thread
 
grahamthedev profile image
GrahamTheDev

The console.log is for demonstration purposes, you can just do:

let myNewVar = last

and then return it or use it or whatever, the only bit that is of significance is:

const {length, [length-1]: last} = str

Which I am still processing as Jon always blows my mind with some weird syntax I haven't seen before (or more precisely have seen but don't use so have to then go and do a load of unpacking of his solution 🀣)!

Thread Thread
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

@labspl - I'm not disagreeing with you, but - again - this is entirely irrelevant to the post, or to my original comment

Thread Thread
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

@inhuofficial I aim to please :)

Collapse
 
jzombie profile image
jzombie

This is awesome

Collapse
 
meirlamdan profile image
meirlamdan • Edited

what you wrote at the end of the post, is not true.
It will not help to copy the array, because it only creates a reference to that array itself
Instead it is possible as follows

let myArray2 = [...myArr]
let last = myArr.pop()
Enter fullscreen mode Exit fullscreen mode
Collapse
 
pengeszikra profile image
Peter Vivo
  const list = ['Apple', 'Orange', 3, 4, 'Football', 'Cricket'];
  list.slice(-1)?.[0]; // 'Cricket'
  'Cricket'.slice(-1)?.[0]; // 't'
  [].slice(-1)?.[0]; // undefined
Enter fullscreen mode Exit fullscreen mode