How to Get the Last Element of an Array in JavaScript

There are several ways to get the last element of an array including length property and square bracket notation, slice() method and pop() method. Here are nine methods to try.

Written by Trung Thanh Le
Published on Mar. 07, 2024
Developer working with manager to review code
Image: Shutterstock / Built In
Brand Studio Logo

Working with arrays is a common task in many programming languages, and it’s necessary to access the last element of an array for a variety of reasons. Whether you need to retrieve the final value in a series of data, or simply want to check the last item in a list, getting the last element of an array can be an important part of your code.

In this article, we will look at the different ways you can access the last element of an array in JavaScript and discuss the importance of this operation in various contexts.

Methods to Get the Last Element of an Array in JavaScript

  1. Length property and square bracket notation
  2. Slice() method
  3. Pop() method
  4. Reduce() method
  5. Spread operator and Destructuring Assignment
  6. ForEach() method
  7. Reverse() method and square bracket notation
  8. Object.keys() function
  9. Array.prototype.at() method

There are different ways of how to get the last element in an array. All of them have their pros and cons. We’ll also present a new and concise method that came with ES6 to get the last element of an array without any side effects.

 

9 Ways to Get the Last Element of an Array in JavaScript

 

1. Length Property and Square Bracket Notation

To get the last element of an array in JavaScript, you can use the length property of the array and subtract one from it. Then, use the square bracket notation to access the element at that index.

For example, given the following array:

const array = [1, 2, 3, 4, 5];

You can get the last element like this:

const last = array[array.length - 1];

This will assign the value 5 to the last variable.

More on JavaScript3 Ways to Find the Maximum Value in a JavaScript Array

 

2. Slice() Method

Alternatively, you can use the slice() method to get a new array that includes only the last element of the original array. For example:

const last = array.slice(-1);

This will create a new array [5] containing the last element of the original array.

 

3. Pop() Method

const last = array.pop();

The pop() method removes the last element of the array and returns it. This method modifies the original array, so if you need to preserve the original array, you should use one of the other methods.

 

4. Reduce() Method

const last = array.reduce((acc, current) => current);

The reduce() method applies a function to each element in the array, starting from the left, and returns a single value. In this case, the function just returns the current element. So, the reduce() method will return the last element of the array.

 

5. Spread Operator (…) and Destructuring Assignment

const [last] = […array].reverse();

The spread operator creates a new array that is a copy of the original array, reversed. It then uses destructuring assignment to assign the first element of the reversed array, which is the last element of the original array, to a variable called last.

 

6. ForEach() Method

let last;
array.forEach(item => {
 last = item;
});

The forEach() method calls a function for each element in the array. In this case, the function assigns the current element to a variable called last, so the last variable will hold the value of the last element in the array when the loop is finished.

 

7. Reverse() Method and Square Bracket Notation

const last = array.reverse()[0];

This reverses the array and then uses square bracket notation to access the first element, which is the last element of the original array.

 

8. Object.keys() Function

const last = array[Object.keys(array)[Object.keys(array).length - 1]];

The Object.keys() function returns an array of the enumerable property names of an object. In this case, it is used to get an array of the indices of the elements in the array. The last element of this array is used to access the last element of the original array.

 

9. Array.prototype.at() Method

const last = array.at(-1);

Last but not least, you can also use the Array.prototype.at() method. In order to get the last item of an array, you can pass in “-1” into the at() method. This is relatively new and was introduced with ES6.

A tutorial on how to get the last element of an array in JavaScript. | Video: KodeBase

More on JavaScript7 Ways to Remove Duplicates From a JavaScript Array

 

What’s the Best Way to Get the Last Element of an Array in JavaScript?

As you can see, there are many different ways to retrieve the last element of an array in JavaScript. Some mutate the original array, which you may not want to do. Others are more verbose. Deciding which one to use depends on your specific case. If your goal is to only get the last element of an array, the Array.prototype.at() method is typically the best choice.

Explore Job Matches.