javascript

Remove First and last Element From Array Javascript


JavaScript has functions that help us manipulate arrays.

Contents

Consider the following array.

var cars = ["Mercedes-Benz", "Toyota ", "Volkswagen", "BMW", "Porsche"];

Remove the First Element From Array

shift() removes the first element of an array and returns the removed element.

cars.shift();
console.log(cars);

Output

["Toyota ", "Volkswagen", "BMW", "Porsche"]

Remove the Last Element From Array

pop() removes the last element of an array and returns the removed element.

cars.pop();
console.log(cars);

Output

["Toyota ", "Volkswagen", "BMW"]

Great!!! Try to remove the first and last by our yourself. Keep learning.

Thanks for reading!


Share on social media

//