Remove the First Element From an Array in JavaScript

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.

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:

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

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *