How to remove the first item from an array in JavaScript
In this tutorial, we will show you how to remove the first item from an array in JavaScript. Here, we’ll explain about the two different methods to delete the first element from an array in JavaScript.
- How to Copy to Clipboard in JavaScript
- JavaScript Convert 12 Hour AM/PM Time to 24 Hour Time Format
- How to lowercase or uppercase all array values in JavaScript
- Check whether a variable is number in JavaScript
1. Using shift() method
The shift() function returns the deleted element after removing the first element from an array. The original array will be updated using this function.
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
The slice() function will be used in the second method to return a shallow copy of an array from start
to end
index. It doesn’t change the original array in any way.
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 all I’ve got for today.
Thank you for reading. Happy Coding..!!