How to get last element of array in JavaScript

In this article, I will going to show on how to get the last element of an array in JavaScript, exploring different methods and provide practical example codes.

Methods to Get the Last Element of an Array in JavaScript

To get the last element of an array in JavaScript, you can use different methods.

Let’s probe each of these methods in details.

Method 1: Using the Index array.length – 1

One of the method to get the last element of an array is by using its index.

In JavaScript, arrays are zero-based, that means the first element is located at index 0.

To access the last element, you can use the index array.length – 1.

Here’s an example code:

const array = [10, 20, 30, 40, 50];
const lastElementExample = array[array.length - 1];
console.log(lastElementExample );

Output:

50

Method 2: Using the pop() Function

Another method to get the last element of an array is by using the pop() function.

This method eliminate the last element from the array and returns it, allowing you to store it in a variable.

Let’s take a look at an example:

const array = [10, 20, 30, 40, 50];
const lastElementFunction = array.pop();
console.log(lastElementFunction);
console.log(array);

Method 3: Using Array Destructuring

JavaScript supports array destructuring, which enables you to extract values from arrays into individual variables.

By using this feature, you can easily get the last element of an array.

Here’s how you can do this:

const arraySample = [11, 12, 13, 14, 15];
const [, , , , lastElementSample] = arraySample;
console.log(lastElementSample); 

Method 4: Using the slice() Function

The slice() function in JavaScript enables you to extract a portion of an array and return it as a new array.

By passing a negative index to slice(), you can extract the last element.

Here’s an example code:

const exampleArray = [10, 20, 30, 40, 50];
const lastElementSample = exampleArray.slice(-1)[0];
console.log(lastElementSample); 

Frequently Asked Questions

How do I check if an array is empty in JavaScript?

To check if an array is empty in JavaScript, you can use the length property. If the length of the array is zero, that means the array is empty.

How can I add elements to an array in JavaScript?

There are several methods to add elements to an array in JavaScript. You can use the push() method to add elements to the end of an array or the unshift() method to add elements to the beginning.

Can I modify the last element of an array directly?

Yes, you can modify the last element of an array directly by accessing it through its index. Since arrays in JavaScript are mutable, you can assign a new value to the last element using the index array.length – 1.

How can I remove the last element from an array?

To remove the last element from an array in JavaScript, you can use the pop() method. This method removes the last element and returns it, enabling you to store it in a variable if needed.

Conclusion

In JavaScript, getting the last element of an array is a frequent operation. By using the methods discussed in this article, you can easily access the last element of an array based on your specific requirements.

Remember, you can use the index, pop() method, array destructuring, or the slice() method to get the last element.

Each method provides its own advantages, so choose the one that fits your coding style and the context in which you are working.

Additional Resources

Leave a Comment