Get The Last Element Of An Array In JavaScript

To get the last element of an array in JavaScript, you can use any of these methods –

  1. Use the Array.at() method – `arr.at(-1)` will give you the last element of an array
  2. Use the Array.slice() method – `arr.slice(-1)[0]` will give you the last element of an array
  3. Use the Array.length property – `arr[arr.length-1]` will give you the last element of an array

Let’s take a look at each of these methods in detail.

get the last element of an array in JavaScript

Array.at() To Get The Last Element Of An Array In JavaScript

Array.at() is a new method introduced in ES7 that allows us to get elements from an array by index.

The syntax for this method is –

arr.at(index)

where,

index – the position of the element in the array (0 for the first element, 1 for the second element and so on)

If you use negative indexes with Array.at(), you can get elements from the end of the array.

For example, `arr.at(-1)` will give you the last element of an array, `arr.at(-2)` will give you the second last element and so on.

So, to get the last element of an array in JavaScript, we can use `arr.at(-1)` –

let arr = ['a', 'b', 'c'];

console.log(arr.at(-1)); // c

Array.slice() To Get The Last Element Of An Array In JavaScript

The Array.slice() method is used to extract a portion of an array and return it as a new array.

The syntax for this method is –

arr.slice(start, end)

where,

start – the index at which to begin extracting elements (0 for the first element, 1 for the second element and so on)

end – the index at which to end extracting elements (the extracted array will not include this element)

If you omit the end parameter, Array.slice() will extract elements from the start index all the way to the end of the array.

For example, `arr.slice(2)` will extract elements from the third index all the way to the end of the array.

If you use negative indexes with Array.slice(), you can get elements from the end of the array.

For example, `arr.slice(-1)` will give you the last element of an array, `arr.slice(-2)` will give you the second last element and so on.

However, there’s a small gotcha with using Array.slice() to get the last element of an array –

let arr = ['a', 'b', 'c'];

console.log(arr.slice(-1)); // ['c']

As you can see, the output is an array with a single element (i.e. the last element of the original array).

If you just want to get the element itself and not an array, you can use arr.slice(-1)[0] –

let arr = ['a', 'b', 'c'];

console.log(arr.slice(-1)[0]); // c

Array.length To Get The Last Element Of An Array In JavaScript

The simplest way to get the last element of an array in JavaScript is by using the Array.length property –

let arr = ['a', 'b', 'c'];

console.log(arr[arr.length-1]) // c

This works because, as we saw earlier, the Array.length property gives us the length of an array (i.e. the number of elements in an array).

So, when we use arr.length-1 in square brackets (i.e. as an index), we’re effectively saying “get the element at the last index of the array”.

And since the last index of an array is always one less than the length of the array, this gives us the last element.

This method is probably the most commonly used method to get the last element of an array in JavaScript.

So, if you’re looking for a quick and easy solution, this is the method you should use.

Conclusion

In this article, we saw how to get the last element of an array in JavaScript using three different methods – the Array.at() method, the Array.slice() method and the Array.length property.

Which method you use will depend on your specific requirements.

If you just want a quick and easy solution, use the Array.length property.

But if you need more flexibility (e.g. if you want to get elements from the end of the array), then use either the Array.at() or Array.slice() method.

I hope this article has helped you and given you some insights into how to get the last element of an array in JavaScript.

As always, if you have any questions or comments, feel free to post them in the comments section below.

Leave a Reply