Code Premix

How to remove the first item from an array in JavaScript

📅April 19, 2022|🗁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.

  1. Using shift() method
  2. Using slice() method

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..!!