How to remove the first element of an array in JavaScript

#javascript #array

Last updated on Nov 17, 2020 by Suraj Sharma

In this tutorial, you will learn two different ways to remove the first element of an array in JavaScript.

1. Using Array.shift() method

The shift() method removes the first element of an array, it updates the original array.


const arr = [1,2,3,4];

arr.shift() // returns 3 (length of arr)

console.log(arr) // [2,3,4]

2. Using Array destructuring

You can use array spread operator(...) and array destructuring to create a new array with the first element removed.


const [firstElement, ...restArr] = [1,2,3,4];

// restArr is a new array without the firstElement

console.log(restArr) // [2,3,4]

console.log(firstElement) // 1

Related Solutions

Suraj Sharma is a JavaScript Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.