Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get last element of array in JavaScript

In this blog post, we will discuss how to get the last element of an array in JavaScript. We will go through different methods of achieving this goal, including using the length property, the slice() method, and the pop() method. We will also provide some practical examples and explain the advantages and disadvantages of each approach. This post is aimed at beginner programmers who are learning JavaScript or anyone who wants to brush up on their array manipulation skills.

What is an Array?

Before diving into the ways we can get the last element of an array, let's discuss what an array is. An array is a data structure in JavaScript that can store multiple values. Think of it as a list or a collection of items, where each item has a specific position called an index. The index starts from 0, meaning the first element of the array has an index of 0, the second element has an index of 1, and so forth.

Here's an example of an array in JavaScript:

const fruits = ['apple', 'banana', 'cherry'];

In this example, the fruits array contains three elements: 'apple', 'banana', and 'cherry'. The index of 'apple' is 0, the index of 'banana' is 1, and the index of 'cherry' is 2.

Method 1: Using the length property

One of the most common ways to get the last element of an array is by using the length property. The length property returns the number of elements in the array. Since the first element's index is 0, the last element's index is always one less than the array's length.

Here's how you can use the length property to get the last element of the fruits array:

const lastElement = fruits[fruits.length - 1];
console.log(lastElement); // Output: 'cherry'

In this example, the length of the fruits array is 3. The last element's index is 2 (3 - 1). Therefore, fruits[2] gives us the last element, which is 'cherry'.

This method is straightforward and works for both small and large arrays. However, one downside is that it might not be as intuitive for beginners since it requires subtracting 1 from the array's length.

Method 2: Using the slice() method

Another way to get the last element of an array is by using the slice() method. The slice() method returns a new array containing a portion of the original array. It takes two arguments: the start index and the end index. It returns the elements from the start index up to (but not including) the end index.

To get the last element of the array, we can use the slice() method with a start index of -1. This will return a new array containing only the last element. Then, we can access the first (and only) element of the new array to get the last element of the original array.

Here's how you can use the slice() method to get the last element of the fruits array:

const lastElementArray = fruits.slice(-1);
const lastElement = lastElementArray[0];
console.log(lastElement); // Output: 'cherry'

In this example, fruits.slice(-1) returns a new array containing only the last element ('cherry'). Then, we access the first element of the new array (lastElementArray[0]) to get the last element of the original array.

This method might be more intuitive for beginners since it doesn't require subtracting 1 from the array's length. However, it might be slightly less efficient than the first method, as it creates a new array.

Method 3: Using the pop() method

The pop() method is another way to get the last element of an array. The pop() method removes the last element from the array and returns it. This method not only retrieves the last element but also modifies the original array by removing the last element.

Here's how you can use the pop() method to get the last element of the fruits array:

const lastElement = fruits.pop();
console.log(lastElement); // Output: 'cherry'
console.log(fruits); // Output: ['apple', 'banana']

In this example, fruits.pop() returns the last element ('cherry') and removes it from the fruits array. The fruits array now contains only 'apple' and 'banana'.

This method is simple and easy to use, but it has the downside of modifying the original array. If you need to keep the original array intact, this method might not be the best choice.

Conclusion

In this post, we have explored three different methods to get the last element of an array in JavaScript:

  1. Using the length property
  2. Using the slice() method
  3. Using the pop() method

Each method has its advantages and disadvantages, depending on your specific use case and programming style. The most important thing is to understand how each method works and choose the one that best suits your needs.

Now you should have a better understanding of how to get the last element of an array in JavaScript. Keep practicing and experimenting with these methods to become more proficient in array manipulation. Happy coding!