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 : 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 item. JavaScript Array Last Element 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: By using the array's length to determine the position of the last element (Which we used in the example before). By using the method. By using the method. 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 method, a mutating method, and the method, a copying method. As mentioned before always changes the length and the content, but on the other hand returns a copy that contains some of the elements from the original array. Access the last element of an array using the slice() method In the previous code we defined an array ( ), then we used the method to get the last element of the array by passing a negative (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. Access the last element of an array using the pop() method In this given code snippet, we are getting the last element of the array using the method. It removes the last element of an array, and returns it. If you pick this approach you must keep in mind that the method modifies the original array. 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. Access the last element of an array using the length of the array In this code, we used the property and the square bracket notation to get the last element of the array. Where the 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). Conclusion 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 . 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 at 4Geeks.com We have a that will help you master it.