Javascript
Arrays
Written by:
There are multiple ways to get the last element of an array in JavaScript, take a look at the following example showing one easy way to get the last item in an array in JavaScript:
1const colorsArray = ['purple', 'yellow', ‘orange’, 'blue' ‘red’, ]; 2const getLastItem = colorsArray[colorsArray.length - 1]; 3console.log('The last element is: ', getLastItem);
The previous code shows how to get the last element of an array by using the array's length, we are using the length property to determine the position of the last element, because the length of the array may vary so you can't use a fixed number, and since the array count always starts at 0, we can pick the last item by referencing the Array.length - 1
item.
There is no built-in property or method created specifically to access the last element of an array, but there are different approaches to get it. The most common methods used are the following:
Array.pop()
method.Array.slice()
method.JavaScript Arrays provides a collection of methods that can be used to get the last item of an array. Some methods provided by JavaScript do not mutate the existing array that the method was called on, but they instead return a new array. They do so by first creating a new array and then populating it with elements. For example, let's take the pop()
method, a mutating method, and the slice()
method, a copying method. As mentioned before pop()
always changes the length and the content, but on the other hand slice()
returns a copy that contains some of the elements from the original array.
1let numbersArray = [7, 23, 73, 41, 51, 18]; 2 3let lastElement = numbersArray.slice(-1)[0]; 4 5console.log(lastElement); 6 7// Expected Output: 18
In the previous code we defined an array (numbersArray
), then we used the slice()
method to get the last element of the array by passing a negative index(-1)
(When using negative numbers it will look for the positions from the end to the beginning). Then it returns a new array with the elements that we specified (In this case the last element), and we accessed to the first and only value of that array.
1const numbers = [13, 44, 87, 18, 10, 08, 144, 16, 28]; 2const getLastItem = numbers.pop(); 3 4console.log(getLastItem); 5 6// Expected Output: 28
In this given code snippet, we are getting the last element of the numbers
array using the pop()
method. It removes the last element of an array, and returns it. If you pick this approach you must keep in mind that the pop()
method modifies the original array. pop()
has the fastest performance, so it is well known as the fastest way to get the last item from an array in JavaScript but, you can only use it if you want to modify the array since this method removes the last element of the array.
1let names = ['Brandon', 'Carolina', 'Randy', 'Rube', 'Andler']; 2let lastElement = names[names.length - 1]; 3console.log(lastElement); 4 5// Expected Output: Andler
In this code, we used the length
property and the [ ]
square bracket notation to get the last element of the array. Where the length
property returns the number of elements in an array, and the [ ]
notation is used to access an element at a specific index in the array, so we got the total number of elements in the array (using the length) and subtract 1 from this value to get the index of the last element (As the position numbers starts from 0).
JavaScript provides an effective collection of methods that are very useful to get the last item in an array. All three approaches shown are valid and can be used in various cases. You can always learn more about JavaScript Arrays. We hope this article has provided you with effective methods to retrieve the last element of an array in JavaScript. Enhance your array manipulation skills by exploring other articles and resources on our blog. If you're looking to broaden your knowledge in JavaScript and web development, feel free to sign up for free at 4Geeks.com We have a JavaScript course that will help you master it.