Categories
JavaScript Answers

How to get the last item of array without pop with JavaScript?

Spread the love

Sometimes, we want to get the last item of array without pop with JavaScript.

In this article, we’ll look at how to get the last item of array without pop with JavaScript.

How to get the last item of array without pop with JavaScript?

To get the last item of array without pop with JavaScript, we can use the array at method.

For instance, we write:

const array = [1, 2, 3, 5, 7];
const last = array.at(-1)
console.log(last)

We call array.at with -1 to return the last entry of array.

Therefore, last is 7.

Conclusion

To get the last item of array without pop with JavaScript, we can use the array at method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *