Get the Last Element of an Array using JavaScript

In this tutorial, we will learn how to use the length, pop(), and slice() methods in JavaScript to get the last element of an Array. The length property allows us to easily find the number of elements in an array or the number of characters in a string. The pop() method allows us to remove the last element of an array. The slice() method allows us to extract a part of an array or string and create a new one.

Get the Last Element of an Array with JS

This tutorial covers three different ways to get the last element of an array: length, pop() and slice().

Table of Contents

length

To get the last element of an array using JavaScript, you can use the length property and the [] square bracket notation. The length property returns the number of elements in an array, and the [] notation allows you to access an element at a specific index in the array.

Here is an example of how you might use the length property and the [] notation to get the last element of an array:

// define an array of numbers
let numbers = [1, 2, 3, 4, 5];

// get the last element of the array
let lastElement = numbers[numbers.length - 1];

// log the last element to the console
console.log(lastElement); // 5

In this example, we define an array of numbers called numbers. We then use the length property to get the total number of elements in the array, and subtract 1 from this value to get the index of the last element. We use the [] notation to access the element at this index, which returns the last element of the array with the value 5.

pop()

Alternatively, you can use the pop() method to remove and return the last element of an array. This method modifies the original array, so keep in mind that it will change the length of the array and shift the index of all other elements.

Here is the same example using pop():

// define an array of numbers
let numbers = [1, 2, 3, 4, 5];

// get the last element of the array
let lastElement = numbers.pop();

// log the last element to the console
console.log(lastElement); // 5

In this example, we use pop() to remove and return the last element of the numbers array. This method returns the value 5, which is the last element of the array. It also modifies the numbers array, so the length of the array is now 4 and the index of the last element is now 3.

slice()

To get the last element of an array using the slice() method in JavaScript, you can pass a negative index as the second argument to slice(). This will return a new array with the elements starting from the end of the original array, rather than from the beginning.

Here is an example of how you might use slice() to get the last element of an array:

// define an array of numbers
let numbers = [1, 2, 3, 4, 5];

// get the last element of the array
let lastElement = numbers.slice(-1)[0];

// log the last element to the console
console.log(lastElement); // 5

In this example, we define an array of numbers called numbers. We then use slice() to get the last element of the array by passing a negative index (-1) as the second argument to slice(). This returns a new array with a single element, which is the last element of the original numbers array. We then access the first element of the new array using the [] notation, which returns the value 5.

Alternatively, you can use slice() to get the last n elements of an array, where n is a positive integer. This will return a new array with the last n elements of the original array, starting from the end of the array.

Here is an example of how you might use slice() to get the last n elements of an array:

// define an array of numbers
let numbers = [1, 2, 3, 4, 5];

// get the last 3 elements of the array
let lastThree = numbers.slice(-3);

// log the last three elements to the console
console.log(lastThree); // [3, 4, 5]

Whether you use the length property, or the pop() and slice() method, will depend on whether you want to modify the original array and whether you need to access the other elements in the array after getting the last element.

All three approaches are valid and can be used in different situations.