JavaScript offers a variety of tools to work with arrays, one of the most basic data types in programming. In this article, we will first learn some simple methods to find the last element in an array in JavaScript.
How to Get the Last Element in a JavaScript Array?
As we know, An array is a collection of similar types of elements i.e. the data type of all elements in an array will be the same. It is an ordered collection, each element has an index.
The index starts from 0 and increases by 1 for every next element. For accessing an element in an array at a particular index, we use square bracket ‘[]’ notation.
Let’s take an example of an array named ‘Ages’. It is a collection of ages of 5 individuals(all numbers). Look at the code below:
// Define an array named 'ages' containing numerical values. let ages = [20, 23, 25, 27, 30]; // Log the entire 'ages' array to the console. console.log(ages); // Retrieve the element at index 2 in the 'ages' array. let eleAt2 = ages[2]; // Log the retrieved element (element at index 2) to the console. console.log(eleAt2);
Output:
[ 20, 23, 25, 27, 30 ]
25
Here we declared an array of length 5. Then we accessed the element at index 2. As indices start at 0 ages[2] will correspond to the third element of the array.
Whether it’s for displaying information, performing calculations, or making decisions based on the last item, understanding ways to get the last element is a valuable skill. No matter if you are a beginner or have some experience in coding, we will cover all the methods easily. Let’s get started.
1) Using the Length Property
The simplest approach to get the last element in an array is by using the JavaScript length property. As we know array.length returns the length of the array. We also know that the index starts from 0, so the index of the last element will be length-1. In this method, we can find the last element by accessing the array[array.length-1] element.
In this case, we have obtained the last element of the array without removing it from the array. Here is an example to better understand it:
// Define an array named 'ages' containing numerical values. let ages = [20, 23, 25, 27, 30]; // Calculate the index of the last element using the array's length property. let lastEle = ages[ages.length - 1]; // Log the last element to the console. console.log(lastEle);
Output:
30
Hence we have 30, which is our desired output.
2) Using Slice Method
The slice() method in JavaScript is used to extract a section of the array and it returns the extracted elements in a new array. Therefore in this method, a new array is made. slice() method takes two parameters in it, which are the starting index and an ending index(optional).
The ending index is not included, only the elements before the ending index are extracted. Moreover, if we don’t pass the ending index then automatically it is assumed to be the last index.
Now the start index can be negative too i.e. -1 means last element,-2 means second last, and so on. Here is an example:
// Define an array named 'ages' containing numerical values. let ages = [20, 23, 25, 27, 30]; // Use the slice() method to create a new array containing the last element. let lastEle = ages.slice(-1); // Log the new array (containing the last element) to the console. console.log(lastEle);
Output:
[ 30 ]
As slice() creates a new array the output is in the form of an array. To get in the form of a single element we can access the first(and the only) element of the new array. Here’s the code :
// Define an array named 'ages' containing numerical values. let ages = [20, 23, 25, 27, 30]; // Use the slice() method to create a new array containing the last element, // and then access that element using [0] to get a single value. let lastEle = ages.slice(-1)[0]; // Log the last element to the console. console.log(lastEle);
Output:
30
The slice() method is also used to remove the last character in a JavaScript string.
3) Using Pop Method
The pop() method gives us the last element of the array and also removes the last element of the array after returning. This method modifies the original array and the length of the array is shortened by one element after every pop() operation.
Let’s implement it practically:
// Define an array named 'ages' containing numerical values. let ages = [20, 23, 25, 27, 30]; // Use the pop() method to remove and retrieve the last element from the 'ages' array. let lastEle = ages.pop(); // Log the retrieved last element to the console. console.log(lastEle); // Log the modified 'ages' array (without the last element) to the console. console.log(ages);
Output:
30
[ 20, 23, 25, 27 ]
As we can see we have got the desired output from the pop() operation. But one more thing to notice is that we have removed the last element from the array. We have now got the modified array when we printed the whole array ages.
4) Using Reverse Method
In this method, we use the reverse() function of JavaScript which is used to reverse the whole array. In this the element that was at the end of the original array is now the first element and the last second element is now the second element and so on. In this method also the original array is modified.
Here is the code:
// Define an array named 'ages' containing numerical values. let ages = [20, 23, 25, 27, 30]; // Use the reverse() method to reverse the order of elements in the 'ages' array. ages.reverse(); // Log the first element of the modified 'ages' array (previously the last element) to the console. console.log(ages[0]);
Output:
30
In this method, we first reversed the array and then we accessed the first element of the array by ages[0].
Here is a simple comparison of all the above methods:
Name of Method | Original array Modified? | New array formed? | Rank Acc to speed |
---|---|---|---|
Length property | No | No | 4 |
slice() | No | Yes | 3 |
pop() | Yes | No | 1 |
reverse() | Yes | No | 2 |
According to speed, we have found that the pop() method is the fastest, but considering other factors is also important while choosing the method. As in pop() and reverse() methods the original array is modified so these can be used where modification is allowed and duplicating the array won’t be a good solution.
On the other hand length and slice() methods don’t modify the original array but are comparatively slower than other methods.
Conclusion
We have discussed four different methods to find the last element of the array in JavaScript. Using the right method ensures the effectiveness and efficiency of our code. If you need more assistance, get help for your JavaScript assignment now from top experts.