Remove the First Element From an Array in JavaScript


Arrays are like containers that hold different pieces of information in JavaScript. Sometimes, you need to remove some elements from the beginning of the container. In this guide, we will show you how to remove the first element from an array in javascript.

How to Remove the First Element From an Array in JavaScript

There are a few approaches to remove the first element from an array in javascript:

  • Approach 1: Using the shift() method
  • Approach 2: Using the splice() method
  • Approach 3: Using the filter() method

Approach 1: Using the shift() method

The shift() method helps us to remove or delete first element from the array. It removes the first element and shifts all remaining elements one position down.

Ezoic

Here is an example of removing first element from the array javascript using shift:

let myArray = [1, 2, 3, 4, 5];

// Remove the first element using shift()
let removedElement = myArray.shift();

console.log("Removed Element:", removedElement);
console.log("Updated Array:", myArray);

Here is explanation:

Ezoic

  • shift() grabs the first item in the array.
  • removedElement holds that item, so you can see what you’ve taken out.
  • myArray is now updated without the first element.

Approach 2: Using the splice() method

The splice() method is like a magic wand for arrays. It can add, remove, or replace elements effortlessly. To remove the first element, set the start index to 0 and the number of elements to remove to 1.

Here is an example of removing first element from the array javascript using Splice:

let myArray = [1, 2, 3, 4, 5];

// Remove the first element using splice()
myArray.splice(0, 1);

console.log("Updated Array:", myArray);

In the example above, splice(0, 1) tells JavaScript to start removing elements at index 0 and remove 1 element, effectively getting rid of the first element in the array.

Ezoic

Approach 3: Using the filter() method

Thefilter method to create a new array (newArr) that includes all elements from the original array (arr) except for the element at index 0.

Here is an example of removing first element from the array javascript using filter():

const arr = [1, 2, 3, 4];
const newArr = arr.filter((element, index) => index !== 0);
// newArr = [2, 3, 4]

Conclusion

That’s it; you have learned how to remove first element from an array in javascript using shift and splice method.

Recommended JavaScript Tutorials

EzoicEzoic

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?