I'm a fan of Open Source and have a growing interest in serverless and edge computing. I'm not a big fan of spiders, but they're doing good work eating bugs. I also stream on Twitch.
I've used var last = arr[arr.length - 1] in the past, but now with destructuring, I typically do const [, last] = someArray.
I'm not sure I'd use at aside from getting the last item in an array since currently I can do
e.g. someArray[1] which is less typing than someArray.at(1) for elements that are not the last item. I probably would have opted for an Array.prototype.last.
Maybe there are use cases for it that I'm missing like composing a bunch of functions.
Not everyone is seeing my addendum to this comment so here it is
Just an update to my initial comment as I typed it out pretty quickly yesterday. const [, last] = someArray will work if the array was only two items. For example, if it's 4 items, this won't work. You'll end up with this.
consta=[1,2,3,4];const[,last]=a;console.log(last);// a === 2
If I wanted to get the last element in the above array, I'd have to do this.
consta=[1,2,3,4];const[,,,last]=a;console.log(last);// a === 2
Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
Yes! It’s like my dream came true when I asked for it in your other post lol.
I've used
var last = arr[arr.length - 1]
in the past, but now with destructuring, I typically doconst [, last] = someArray
.I'm not sure I'd use
at
aside from getting the last item in an array since currently I can doe.g.
someArray[1]
which is less typing thansomeArray.at(1)
for elements that are not the last item. I probably would have opted for anArray.prototype.last
.Maybe there are use cases for it that I'm missing like composing a bunch of functions.
Not everyone is seeing my addendum to this comment so here it is
Just an update to my initial comment as I typed it out pretty quickly yesterday.
const [, last] = someArray
will work if the array was only two items. For example, if it's 4 items, this won't work. You'll end up with this.If I wanted to get the last element in the above array, I'd have to do this.
That post is far too noisy. I lost track of comments many days ago.