Javascript get last element of array

In this article step by step explain of how to get last element of array using javascript. I explained simple example about javascript get last element of array. You can see get last index value of array in javascript. In this article, we will look at the different method you can access the last element of an array in Javascript and discuss the imprtance of this operation in various contexts.

Methods to get last element of array in javascript

The steps involved to get the last element of an array 

  1. Use length method to return a specific element
  2. Use array slice() to return a specific element
  3. Retrieve the last element using reverse() method.
  4. Get last element of array using pop() method.
1. Length property and square bracket notation

The length property return the number of elements in array. Substring 1 from the length of an array gives the index of last element of an array using which the last element accessed. Javascript the array index numbering start with 0. So last element ‘s index would be array length -1.

You can get last element of array like below example.

const array = [1, 3, 5, 7, 9];

const lastElement = array[array.length - 1];

console.log(lastElement);

// Output: 9
  • array.length gives the total number of elements in the array.
  • Subtracting 1 gives the index of the element, as arrays are zero-indexed.
2. Javascript get last element of array using slice() method

The slice() method return specific elements from an array, as a new array object. This method select the element starting at the given start index and ends index excluding the element at the last index. slice() method does not modify the existing array. Positive value providing return the element at that position and negative index value calculate the index from the end of the array.

const array = [1, 3, 5, 7, 9];

const lastElement = array.slice(-1)[0];

console.log(lastElement);

// Output: 9
  • slice(-1) creates a new array containing the last element.
  • [0]  is used to access the element from the new array.
3. Javascript get last element of array using reverse() method

To get the last element of an array using the reverse() method, you reverse the array and access the first element of the reversed array. The reverse() method reverses the order of elements in the original array. After reversing, the last elements becomes the first element. 

Example
const array = [1, 3, 5, 7, 9];

const reversedArrayValue = array.reverse();
const lastElement = reversedArray[0];

 console.log(lastElement);
 
 // Output: 9

This reverses the array and then uses square bracket notation to access the first element, which is the last example of the array.

  • The reverse() method changes the order of the elements in the array.
  • [1, 3, 5, 7, 9] becomes [9, 7, 5, 3, 1].
  • After reversing, the element at index is the original last element of the array. reversedArrayValue[0] returns 9.
4. Javascript get last element of array using pop() method

The pop() method removes the last element of the array and returns it. This method modifies the original array, so if you need to get last element to use that method. The pop() method in javascript gets the last element of the array and also alters the length of an array after execution gets done.

const array = [1, 3, 5, 7, 9];

const lastElement = array.pop();

console.log(lastElement);

// Output: 9
  • Use this method if you need to remove the last element from the array.
  • This modifies original array.
5. Javascript get last item of array using at() method

Another option is using the new Array.prototype.at() method which takes an integer value and returns the item at that index. Negative integers count back from the last item in the array so if we want the last item we can pass in -1.

const array = [1, 3, 5, 7, 9];

const lastElement = array.at(-1);

console.log(lastElement);

// Output: 9
  • array.at(-1) directly accesses the last element without calculating the index.

I hope this tutorial help you.

Leave a Reply

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