Remove the first element from an array in JavaScript
📅January 19, 2021
In this short article, we will show you how to remove the first element from an array in JavaScript. Here, we’ll discuss about the two different methods to remove the first element from an array in JavaScript.
You may also like the following articles.
- How to merge arrays in JavaScript
- Remove a specific item from an array in JavaScript
- Push, Pop, Shift and Unshift Array Methods in JavaScript
Remove the first element from an array
1. Using shift() method
The shift() method will remove the first element from an array and return that removed element. This method will update the original array.
var arr = ["item 1", "item 2", "item 3", "item 4"];
arr.shift(); // item 1
console.log(arr);
// ["item 2", "item 3", "item 4"]
2. Using slice() method
In the second method, we will use the slice() method to return the shallow copy of an array that selected from `start` to `end` index. It doesn’t modify the original array.
var arr = ["item 1", "item 2", "item 3", "item 4"];
const newArr = arr.slice(1);
console.log(newArr);
// ["item 2", "item 3", "item 4"]
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂