Categories
JavaScript Answers

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

Spread the love

Sometimes, we want to remove first and last element in array with JavaScript.

In this article, we’ll look at how to remove first and last element in array with JavaScript.

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

To remove first and last element in array with JavaScript, we can use the array slice method.

For instance, we write

const newFruits = fruits.slice(1, -1);

to call fruits.slice with 1 and -1 to return a new array without the first and last entries of fruits.

Then we assign the returned array to newFruits.

Conclusion

To remove first and last element in array with JavaScript, we can use the array slice 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 *