How to get the last element of an Array in JavaScript

In JavaScript, an array is a list of values that can be of any data type, including numbers, strings, objects, and even other arrays. Arrays are a common data structure used in JavaScript, and it is often useful to access the last element of an array. In this article, we will go over several ways to get the last element of an array in JavaScript.

One way to get the last element of an array is to use the length property of the array. The length property returns the number of elements in the array. We can use this property to access the last element of the array by subtracting 1 from the length and using the result as the index for the array. For example:

const array = [1, 2, 3, 4, 5];
const lastElement = array[array.length - 1];  // 5

Another way to get the last element of an array is to use the pop() method. The pop() method removes the last element of an array and returns it. For example:

const array = [1, 2, 3, 4, 5];
const lastElement = array.pop();  // 5

The pop() method is a convenient way to get the last element of an array, but it has the side effect of modifying the original array. If you need to preserve the original array, you can use the slice() method to create a copy of the array and then use the pop() method on the copy. For example:

const array = [1, 2, 3, 4, 5];
const copy = array.slice();
const lastElement = copy.pop();  // 5

Another way to get the last element of an array is to use the reduce() method. The reduce() method applies a function to each element of the array and accumulates the result. We can use the reduce() method to get the last element of the array by providing a function that returns the current element if it is the last element in the array, and otherwise returns the accumulator. For example:

const array = [1, 2, 3, 4, 5];
const lastElement = array.reduce((acc, curr) => (array.indexOf(curr) === array.length - 1 ? curr : acc));  // 5

The reduce() method is a powerful tool for manipulating arrays, but it may not be the most efficient way to get the last element of an array. If performance is a concern, the length or pop() methods may be more suitable.

Another option for getting the last element of an array is to use the lastIndexOf() method. The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if the element is not present. We can use this method to get the last element of the array by passing in the last element of the array as the argument. For example:

const array = [1, 2, 3, 4, 5];
const lastElement = array[array.lastIndexOf(array[array.length - 1])];  // 5

Alternatively, we can use the reverse() method to reverse the array and then use the pop() method to remove the first element of the reversed array. For example

const array = [1, 2, 3, 4, 5];
const reversed = array.reverse();
const lastElement = reversed.pop();  // 5

It is worth noting that the reverse() method modifies the original array, so if you need to preserve the original array, you can use the slice() method to create a copy before reversing it.

Another way to get the last element of an array is to use the for...of loop. The for...of loop allows us to iterate over the elements of an array without the need for an index. We can use this loop to get the last element of the array by iterating over the array and storing the current element in a variable. For example:

const array = [1, 2, 3, 4, 5];
let lastElement;
for (const element of array) {
  lastElement = element;
}

The for...of loop is a simple and efficient way to get the last element of an array, but it may not be the most concise solution.

Finally, we can get the last element of an array using the find() method. The find() method returns the first element in the array that satisfies a given predicate function. We can use this method to get the last element of the array by providing a predicate function that checks if the current element is the last element of the array. For example:

const array = [1, 2, 3, 4, 5];
const lastElement = array.find((element, index) => index === array.length - 1);  // 5

These are just a few examples of how to get the last element of an array in JavaScript. Depending on your needs and preferences, you may find one solution more suitable than the others. It is important to consider factors such as performance, readability, and maintainability when choosing the best solution for your problem.

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts

What is AngularJS

AngularJS, a powerful open-source JavaScript framework, has gained immense popularity among developers since its introduction by Google in 2010. Designed

Read More