Categories
JavaScript Answers

How to get the last item in an array with JavaScript?

Spread the love

To get the last item in an array with JavaScript, we use the at method.

For instance, we write

if (locArray.at(-1) === "index.html") {
  // ...
} else {
  // ...
}

to get the last item from the locArray array with locArray.at(-1).

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 *