Getting to the end
The many, many ways to get to the end of an array
Someone recently asked me what the best way to get the last item in array is. It’s a simple question, but just like everything else in JavaScript there are a ridiculous number of ways to accomplish the same thing. I thought I’d list all of the (some useful, some insane) ways I could think of to “get the last item in an array”
1: Use .slice() with destructuring assignment on the array.
2: Use the arrays index and length.
3: Use .pop on the array.
4: Use .reverse() with destructuring assignment on the array.
5: Use .reverse() with .shift() on the array.
6: Use .filter(), index, length and destructuring assignment on the array.
These are all of the ways I could think of off the top of my head, I’d love to hear more useful and ridiculous ways to get the last item in an array. So please post them up in the comments!