How to get last item in array javascript?

Software
AffiliatePal is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Listen

Introduction

When working with arrays in JavaScript, it is common to need to access the last item in the array. Whether you want to retrieve the last element for further processing or simply display it, there are several ways to accomplish this task. In this article, we will explore different methods to get the last item in an array using JavaScript.

Using the length property

One straightforward way to get the last item in an array is by using the length property. The length property returns the number of elements in an array, and since arrays are zero-indexed, the last item can be accessed by subtracting 1 from the length.

Example:
“`javascript
const array = [1, 2, 3, 4, 5];
const lastItem = array[array.length – 1];
console.log(lastItem); // Output: 5
“`

In the example above, we have an array with five elements. By subtracting 1 from the length (which is 5), we can access the last item in the array using the resulting index (4).

Using the pop() method

Another way to get the last item in an array is by using the pop() method. The pop() method removes the last element from an array and returns that element. This method modifies the original array.

Example:
“`javascript
const array = [1, 2, 3, 4, 5];
const lastItem = array.pop();
console.log(lastItem); // Output: 5
console.log(array); // Output: [1, 2, 3, 4]
“`

In the example above, the pop() method is used to remove the last element (5) from the array and assign it to the lastItem variable. The modified array is then logged to the console, showing that the last element has been removed.

Using the slice() method

The slice() method can also be used to get the last item in an array. The slice() method returns a shallow copy of a portion of an array into a new array object. By passing a negative index (-1) as the starting point, we can retrieve the last item in the array.

Example:
“`javascript
const array = [1, 2, 3, 4, 5];
const lastItem = array.slice(-1)[0];
console.log(lastItem); // Output: 5
“`

In the example above, the slice() method is used with a negative index (-1) to create a new array containing only the last item. The [0] at the end is used to access the first (and only) element in the new array, which is the last item in the original array.

Conclusion

In conclusion, there are multiple ways to get the last item in an array using JavaScript. You can use the length property to access the last item by subtracting 1 from the length and using the resulting index. Alternatively, you can use the pop() method to remove the last element and retrieve it. Lastly, the slice() method can be used to create a new array containing only the last item. Choose the method that best suits your needs and coding style.

References

– developer.mozilla.org: Array.prototype.length – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length
– developer.mozilla.org: Array.prototype.pop() – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop
– developer.mozilla.org: Array.prototype.slice() – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice