Javascript remove first element from array example; In this tutorial you will learn how to remove the first element from an array in JavaScript.
In javascript we can remove the first item from an array two ways:
1. Using shift() Method
The shift method removes the first element from an array and returns the removed element.
const arr = ['a', 'b', 'c'];
const firstElement = arr.shift();
console.log(firstElement); // a
console.log(arr); // ['b', 'c']
2. Using slice() Method
We can use the Array.slice method to get a new array, containing all array elements, but the first.
const arr = ['a', 'b', 'c'];
const withoutFirst = arr.slice(1);
console.log(withoutFirst); // ['b', 'c']
console.log(arr); // ['a', 'b', 'c']
The Array.slice method is very different from Array.shift because it does not change the contents of the original array.