Categories
JavaScript Answers

How to get the second to last item of an array with JavaScript?

Spread the love

To get the second to last item of an array with JavaScript, we use the length subtract by 2 index.

For instance, we write

const pgUrl = array[array.length - 2];

to get the 2nd last item in array with index array.length - 2.

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 *