How to get last element in array using JavaScript (with examples)?
Are you wondering how to get the last element of an array in JavaScript? JavaScript has a lot of built-in methods that can be used to manipulate arrays. Let’s find out what we can implement to access the last element of the array.
Using length property
Suppose, you have an array and you know the length of that array.
const animals = ['giraffe', 'lion', 'deer', 'monkey']
Code language: JavaScript (javascript)
We are given an array named animals
whose length is 4. To get the first element, we use animals[0]
(array indexing starts from 0), for the second element- animals[1]
, and so on. Suppose the array is a little larger and we want to traverse the whole array and execute some code, so for that, we need to start a loop from the 0th index to the last index of the array, like this –
for(let i=0;i<animals.length;i++)
{
//code to be executed
}
Code language: JavaScript (javascript)
To get the length of the array animals
we use animals.length and to get the last element of the array, we can use the length
property. Since array indexing starts from 0, we need to reference the array with index animals.length-1.
For example-
const lastElement = animals[animals.length-1];
Code language: JavaScript (javascript)
lastElement
consists of the last element of the array animals
.
Using slice() method
When we want to retrieve some data from an array without modifying the original array, we use Array.slice()
method.
It has a very simple syntax –
ArrayName.slice(indexFrom, indexTo)
Here, ArrayName
is the name of the array from which you’re trying to retrieve the data, indexFrom
is the index from where you want to start slicing, and indexTo
is the index till which you want to slice. indexFrom
is inclusive and indexTo
is exclusive i.e. the element present at indexFrom
will be returned while the element present at indexTo
will not be returned. If we provide one index value to the slice method, it will return the element at that position. Providing a negative index returns the last element of the array.
let numbers = [1, 3, 5, 7, 9, 11, 13, 15];
let lastElement = numbers.slice(-1);
console.log(lastElement);
//Output: 15
Code language: JavaScript (javascript)
Using pop() method
The pop()
method modifies the length of the array and removes and returns the last element of the array.
let numbers = [1, 3, 5, 7, 9, 11, 13, 15];
let lastElement = numbers.pop();
console.log(lastElement);
//Output: 15
Code language: JavaScript (javascript)
The length of the array numbers
, after popping the last element now becomes 7.
Using the reverse() method
Another way is by reversing the array with reverse()
method. This way, the last element will become the first element and we can easily reference it.
For example –
const numbers = [1, 3, 5, 7, 9, 11, 13, 15]
console.log(numbers.reverse())
console.log(numbers[0])
//OUTPUT- 15, 13, 11, 9, 7, 5, 3, 1
//OUTPUT- 15
Code language: JavaScript (javascript)
Conclusion
We saw different ways to access the last element of the array using JavaScript.
The most intuitive method would be the first one in which we used the length
property to access the last element. It is also the most efficient one as we don’t have to modify the original array.
In the pop()
method we remove the last element so this modifies the original array, reducing its length by 1. There might not be a case where modification of the original array is allowed and duplicating the original array would not be an efficient solution. slice()
is also an efficient method for this as we don’t have to modify the original array.
The reverse()
method should be avoided as it also modifies the original array.
It might be a case that you have some different intuition to access the last element and you can very well use it but remember the most important aspect should be efficiency, check your method’s efficiency, and then implement it.
Thank you for reading!
Sharing is caring
Did you like what Vineeta Tiwari wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: