DEV Community

Discussion on: Get last element of array in JavsScript

 
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 :)