Get the Last Element of an Array Using JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

An array is like a container that contains elements of the same data type in it. These elements can be accessed later in the program by using the index number of these elements, for various purposes of the user. Sometimes we need to access the last element from an array, for which we use various methods in javascript. In this article, we will learn in javascript get the last element of the array. All the topics are covered below with examples and explanations where ever necessary.

Introduction

The last element in the array or list can be accessed from an array by using different methods. In javascript, the type of elements in an array does not need to be defined because javascript is a weakly-typed or untyped language. Javascript automatically determines the data type of the elements of an array when it reads the program. An array list is defined by using the big bracket and []. The elements inside this bracket are separated by a comma (,).

For example, lets us suppose an array where we will store three names inside the array namely firstName. The string values inside the array are stored inside double inverted commas (" ").

How to Get the Last Element of an Array?

There are various methods in javascript to get the last element of the array. We are going to discuss them below. Each of the methods is provided with an example code and explanations for them.

Using the Array Length Property

The length property of javascript returns a value that represents the number of elements from a particular array. In javascript, we can easily access any element of an array or a list. For accessing that element from an array, we need to pass the index value of that particular element in a square bracket. Since the indexing starts from 0 in javascript, we need to write (length-1) to get the last element from an array or list in javascript. Let us see an example.

Output:

Explanation: In the above example, we created an array list and inserted many elements (integer type) in the list. Then we created a variable lastElement. After that, we used the length property and appended it to the list namely myList. Since the indexing of the array starts from 0 in javascript. For getting the last element from an array, we need to pass the index value List -1 to the list. Then we used the console.log to print the output that is 15.

Using the slice() Method

The slice() method is used to return a specific element from an array. The returned array works as a new array object. The slice() method is used for slicing the single or multiple elements from a string index value to the end index provided by the user in the method. If we provide a negative index then the method returns that element from the ending of the array. Let us see an example.

Output:

Explanation: In the above example, we created an array list and inserted many elements (integer type) in the list. Then we created a variable lastElement. Then we used the slice() method to return the element from the array. As we know, when we pass a negative value with the slice method, it returns the last element from the array list. Then we used the console.log method to return the last element which is 15.

To learn more about slice() method in javascript, click here- Slice() method in JavaScript.

Using the pop() Method

The pop() method in javascript gets the last element of the array and is used to access the last element provided in the array. The pop() method in javascript gets the last element of the array and also alters the length of an array after execution gets done. Suppose we have five elements in an array and we use the pop() to get an element. Then after the pop() method works, the new length of the array will be 4. Let us see an example.

Output:

Explanation: In the above example, we created an array list and inserted many elements (integer type) in the list. Then we created a variable lastElement. Then we used the pop method to pick out the last element and return it. Then we used the console.log to print the output that is 15. Use this JavaScript Compiler to compile your code.

Using reverse() Method

The reverse() method in javascript gets the last element of the array is used to reverse all the given elements of an array. Let us see an example.

Output:

Explanation: In the above example, we created an array list and inserted many elements (integer type) in the list. Then we created a variable lastElement. Then we used the reverse() method to reverse the myList. After reversing, the element at index value 0 becomes the last element of the given array and the last element becomes the element at index value 0. Then we used the console.log to print the output and passed the index value0 to the variable lastElement. This prints the last element of the array which is 15.

To earn more about the reverse() method in javascript, click here- Reverse() method in JavaScript.

Checking Which Method is the Fastest

All the above methods are well and good and they all have their own scope and usage. But now we will compare them on the basis of fast execution. And will decide which is the fastest method to get the last element of an array in javascript. For this, we will run all the methods simultaneously and see the compilation time of each.

Output:

Explanation: As we can see, the array length property method takes 3.993 ms to return the last element of the array. The slice() method takes 1.801 ms to return the last element of the array. The pop() method takes 0.093 ms to return the last element of the array. And the reverse() method takes 0.356 ms to return the last element of the array. The execution time of the methods is as Method A > Method B > Method D > Method C. So, the fastest method that returns the last element from the array in javascript is "Method C" which is the pop() method.

Conclusion

  • The last element of an array or list in javascript can be accessed by using the various methods in javascript to get the last element of the array and can be later used in the program as per the user's need.
  • These elements can be accessed by the index number of the elements.
  • The index number in javascript starts from 0, so the index number of the first element of an array will be 0 and the index number of the second element of an array will be 1, and so on.
  • An array list is defined by using the big bracket and []. The elements inside this bracket are separated by a comma (,).
  • For accessing the last element from an array using the length property of javascript, we need to append length- 1 with the list name.
  • The slice() method is used to return a specific element from an array. The returned array works as a new array object.
  • The pop() method is used to access the last element provided in the array. The pop() method also alters the length of an array after execution gets done.
  • The reverse() method in javascript get the last element of the array used to reverse all the given elements of an array and then we access the first element of the reversed array. In this way, we get the last element of an array in javascript by using the reverse() method.