Javascript: Get last element of an array

While working in javascript, often there is a requirement to get the last element of an array. This article will discuss accessing the last element from a javascript array.

Table of Contents:-

Get first element of an array using [ ]

Example:-

Get the last element of the array [“Javascript”, “Is”, “Popular”,”Language”]

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let lastValue = stringArray[stringArray.length-1];
console.log("Last element of array is: " + lastValue );

Output:-

Last element of array is: Language

Explanation:-

  • Here, the last element of an array is retrieved by accessing the element at the length-1 index within the [ ].
  • Note that the index is zero-based in arrays.

Get first element of an array using pop()

Javascript’s pop() method will remove the last element from the array and will return the same element. This method will modify the array.

Example:-

Get the last element of the array [“Javascript”, “Is”, “Popular”,”Language”]

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let lastValue = stringArray.pop() ; //modifies the original array and will remove the last element from array
console.log("Last element of array is: " + lastValue ); 

Output:-

Last element of array is: Language

Explanation:-

  • Here, the pop() method will retrieve the last element of the array in variable lastValue.
  • Note that the pop() method will modify the original array. That is after implementing pop() the elements left in stringArray will be [ “Javascript”, “Is”, “Popular” ]

Get first element of an array by reversing the array

Javascript’s reverse() method will reverse the array that is the last element becomes the first element.

Example:-

Get the last element of the array [“Javascript”, “Is”, “Popular”,”Language”]

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let lastValue = stringArray.reverse()[0];
console.log("Last element of array is: " + lastValue );

Output:-

Last element of array is: Language

Explanation:-

  • Here, the reverse() method will reverse the array, and then the element at 0th index position is retrieved in the variable lastValue.

Get first element of an array using slice()

Javascript’s slice(startIndex, endIndex) method returns a shallow copy of a portion of the calling array into a new array object based on indexes passed as arguments. The index is zero-based.

  • startIndex: is the index to start the extraction.The extraction includes the element at startIndex.
  • endIndex: is the index to end the extraction. The extraction excluded the element at endIndex. This argument is optional; therefore, if not present, it means to extract array elements till the end of the array.
  • If any of the indexes are negative, the offset will start from the end.

Example:-

Get the last element of the array [“Javascript”, “Is”, “Popular”,”Language”]

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let lastValue = stringArray.slice(stringArray.length-1);
console.log("Last element of array is: " + lastValue );

Output:-

Last element of array is: Language

Explanation:-

  • Here, stringArray.slice(stringArray.length-1) is used to extract a copy of a portion of the string from the length -1 index element that is the last.
  • Note that the index is zero-based in arrays.

OR

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let lastValue = stringArray.slice(-1).pop();
console.log("Last element of array is: " + lastValue );

Output:-

Last element of array is: Language

Explanation:-

  • Here, slice(-1) signifies that the offset will start from the end, and hence when the pop() method is applied, the last element of the array is retrieved.

Read More:

I hope this article helped you to get the last element from an array in javascript. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top