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.
The shift()
method removes the first element of an array, it updates the original array.
Example:
const arr = [1,2,3,4];
arr.shift() // returns 3 (length of arr)
console.log(arr) // [2,3,4]
You can use array spread operator(...) and array destructuring to create a new array with the first element removed.
Example:
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
How to check if a string starts with a substring in JavaScript
How to remove all occurences of an element from an array in JavaScript
Rate this post
Suraj Sharma is the founder of Future Gen AI Services. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.