JavaScript Get Last Element of Array

JavaScript Get Last Element of Array | In this article, we look at various approaches to finding the final element of an array. You should read the whole article if you are new to programming or JavaScript. However, if all you want is the code, you may skip to the section below, has it?

A container object called an array carries a predetermined number of values of a single kind. Once constructed, the length of an array would not change. Now that we understand the basics of an array let’s discover the final element in an array.

JavaScript Get Last Element of Array Using Index

If you know the array’s size, you should use index placement. Let’s make an array first:-

const animals = ["cat", "dog", "horse"];

We can use the last item’s index to reach it and acquire it:-

const lastElement = animals[2];

Zero-indexed arrays are used in JavaScript. Because of this, animals allow us to access the “horse” element[2]. We’ll explain what zero-indexed means later in this session if you’re unclear about what it implies.

If you know the array’s length, you can use this technique to find the last item in the array. What happens if you must be aware of the array’s size? This collection of animals is relatively modest. However, you might need to be mindful of the length of another array that contains dozens of objects.

When you don’t know an array’s length, here’s how to acquire its last item: array[array.length – 1].

const animals = ["cat", "dog", "horse"];
const lastElement = animals[animals.length - 1];
console.log("Last element: " + lastElement);

Output:-

Last element: horse

The length property returns the length of this array, in case you are unfamiliar. As there are 3 elements in the array, 3 is printed.

JavaScript arrays, as we previously discovered, are zero-indexed. This indicates that you begin counting in JavaScript arrays from zero rather than one. By taking a glance at our array of animals, we can notice this. Index 0 corresponds to the “cat”, index 1 to the “dog”,  and index 2 to the “horse”.

You could still be perplexed. The animals.length, which we recently discovered, will inform us of the number of objects in an array. Additionally, we found that when using JavaScript arrays, we begin counting at 0 rather than 1.

JavaScript Get Last Element of Array Using pop() Method

The JavaScript Array pop() method fetches and deletes the last element from an array. So, be careful while using pop() method because it will not only return the last element but also delete the last element. Hence it is not recommended to use the pop() method to get the last element of array.

const planets = ["Mercury", "Venus", "Earth", "Moon"];
const lastElement = planets.pop();

console.log("Last element: " + lastElement);
console.log("New array: " + planets);

Output:-

Last element: Moon
New array: Mercury, Venus, Earth

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *