How to get the last element in an array in JavaScript?
Working with arrays is a common task in many programming languages, and often it is 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.
This is just one out of many articles about IT. Feel free to follow and subscribe to pandaquests for more great content about JavaScript, web development, and software development. We try to publish multiple times a week. Make sure not to miss any of our great content.
There are different ways of how to get the last element in an array. All of them have there pros and cons. Read till the end, because we will also present a new and concise method, that came with ES6, of how to get the last element of an array without any side effects.
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 1 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.
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.
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.
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.
spread operator (…) and destructuring assignment
const [last] = […array].reverse();This creates a new array that is a copy of the original array, reversed, and 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.
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.
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).
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.
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 reltively new and came with ES.
As you can see, there are different ways of how to retrieve the last element. Some mutate the original array, which you may not want. Others are more verbose than others. Which one depends on your specific case. If your goal is to only get the last element of an array, the Array.prototype.at() method is for most cases a good choice.

There you have it. We hope you enjoyed this article. If you have any questions, let us know and comment below.
We are publishing multiple articles per week. In order not to miss any of them, follow and subscribe to pandaquests. If you want to support us directly, you can either tip or apply for becoming member with this link. By using that link, 50% of your fee will go directly to us. Only with your generous support we can retain the frequent and high quality of our articles. Thanks in advance and happy coding!






