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.