How to get the last element in JavaScript array?



In this tutorial, we will try to get the last element in an array using the following three approaches −

  • Using Array length Property.

  • Using the slice() Method.

  • Using the pop() Method.

Using Array length Property

The length property of an array returns the number of elements in an array. The array index will start from 0, and the last index will total the length of array -1. For example, if the length of the array is 5 the indexing numbering will be 0 to 4. That means if we want to get the last element, the index will be array length-1.

Syntax

Users can follow the syntax below to get the length using the array length property.

array[array.length - 1];

In the above syntax, we get the total length of the array using the array.length method then decreases it by 1 to get the last element as the index starts from 0 to length-1.

Example

In the below example, we find the last element in an array. We use array length property to find the number total elements in the array.

<html> <body> <div id = "result1">Original Array:</div> <div id = "result2">Last Element:</div> <script> let arr = [2, 4, 6, 8, 10, 12]; document.getElementById('result1').innerHTML += arr; let element = arr[arr.length - 1]; document.getElementById('result2').innerHTML += element; </script> </body> </html>

Using the slice() Method

In JavaScript, we use the slice method to return specific parts of elements from an array. This method takes two parameters, the starting index and the ending index (excluding the element of the end index). This method does not modify the original array. If we enter only one index value, it will return that indexed element only. We can give the array elements negative indexing too. In that case, the last element will be detected as index value -1, then -2, and so on.

Syntax

Users can follow the syntax below to get the length using template literal.

array.slice(-1);

In the above syntax, we use the array.slice() method and got the last element using negative indexing (-1).

Example

In the below example, we apply the slice() method to find the last method in a JavaScript array.

<html> <body> <p>Getting the last element in an array using the slice() method</p> <div id = "result1">Original Array: </div> <div id = "result2">Last Element: </div> <script> let arr = [2, 4, 6, 8, 10, 12]; document.getElementById('result1').innerHTML += arr; let element = arr.slice(-1); document.getElementById('result2').innerHTML += element; </script> </body> </html>

Using the pop() Method

The pop() method removes the array's last element and returns the value. This method modifies the array by decreasing the array length by 1.

Syntax

Users can follow the syntax below to get the length using the String object.

array.pop();

In the above syntax, we use the array.pop() method removes the last element and stores that element in a variable.

Example

In the below example, we used the pop() method to get the last element in an array. The pop() method removes the last element from the array. To get the original element we should store the original element in a temp variable.

<html> <body> <p>Getting the last element in an array using the pop() method</p> <div id = "result1">Original Array: </div> <div id = "result2">Last Element: </div> <script> let arr = [2, 4, 6, 8, 10, 12, 13, 34]; document.getElementById('result1').innerHTML += arr; let element = arr.pop(); document.getElementById('result2').innerHTML += element; </script> </body> </html>

In this tutorial, we discussed three approaches to get the last element in an array. The first approach is to use array length property. The next approach is slice() method and the last approach is to use the pop() method.


Advertisements