. Advertisement .
..3..
. Advertisement .
..4..
Looking to get last element of array JS? Are you a beginner finding help to crack the code? Well, the array is the main part of JavaScript, which is used in most of the coding, be it simple or complex. When working on a program using an array, you will enjoy the JavaScript coding if you understand the topic.
Let’s shed light on getting the last element of array JS
How to Get Last Element of Array JS?
An array is like a container that contains a fixed number of values of a type. There are different ways to get the last element of an array. Check it out
slice() Method
This method is known to return a specific array’s element as a new array object. The existing array can’t be modified. With the slice()
method, you can easily get the last element.
let array = [3, 5, 7, 9, 11, 13, 15, 17];
let lastElement = array.slice(-1);
console.log(lastElement);
Output
17
Array Length Property Method
The Array Length property is a method that can return the element’s number in an array. The main part of this method is subtracting 1 from the length to return the last element of an array.
The purpose of subtraction 1 from the length is that the array index number starts with 0, which means that the index of 1st element would be 0, and hence, the last array’s index would be length-1
.
let array = [3, 5, 7, 9, 11, 13, 15, 17];
let lastElement = array[array.length - 1];
console.log(lastElement);
Output
17
pop() Method
As its name suggests, it pops or removes the last array’s element, and then returns it. The pop()
method can change the length of an array.
let array = [3, 5, 7, 9, 11, 13, 15, 17];
let lastElement = array.pop();
console.log(lastElement);
console.log(array);
Output
17
3, 5, 7, 9, 11, 13, 15
Performance of All Methods
Now let’s compare the three methods to check which one is the fastest one to get the last element of array JS.
let array = [3, 5, 7, 9, 11, 13, 15, 17];
console.time('array length property method');
let lastElement1 = array[array.length - 1];
console.log(lastElement1);
console.timeEnd('array length property method');
console.time('array slice method');
let lastElement2 = array.slice(-1);
console.log(lastElement2);
console.timeEnd('array slice method');
console.time('array pop method');
let lastElement3 = array.pop();
console.log(lastElement3);
console.timeEnd('array pop method');
Output
//17
//array length property method: 13.798ms
//17
//array slice method: 8.839ms
//17
//array pop method: 0.138ms
After comparing the slice()
, pop, and length method, it is evident that pop()
is the fastest method to get the last element of an array. If you want or can modify the array, then you can use this method. If you are not fine to change the array, you can use the slice()
method.
Conclusion
In this tutorial, we have discussed the methods to get the last element of an array along with checking the performance of all three methods. You can use any of the methods to make the program work.
I hope you succeed in this JavaScript program!
Leave a comment