Categories
JavaScript Answers

How to remove first and last element in a JavaScript array?

Spread the love

To remove first and last element in a JavaScript array, we use shift to remove the first item and pop to remove the last item.

For instance, we write

const fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.shift();
fruits.pop();

to call fruits.shift to remove the first item from fruits.

And we call fruits.pop to remove the last item from fruits.

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 *