HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Get the last element of a JavaScript array

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
lastjavascriptelementgetthearray

Problem

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.
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().

Solution

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


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().

Code Snippets

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

Context

From 30-seconds-of-code: last-element-of-array

Revisions (0)

No revisions yet.