JS: shift() Method

The<span style="color:#cf2e2e" class="tadv-color"> pop()</span> method in JavaScript removes an element from the rear of the element.

What if we want to remove element from the front of the JavaScript array?

The JavaScript has a special method in the array class that removes just one element from the front of the array. It is the<span style="color:#cf2e2e" class="tadv-color"> shift()</span> method.

In this example, we will create an array and use the <span style="color:#cf2e2e" class="tadv-color">shift()</span> method to remove an element from the front.

myArray = [200,56,100,44,76];
myArray.shift();
console.log(myArray);

In the above example, the <span style="color:#cf2e2e" class="tadv-color">shift()</span> method will remove the first element from the front which is 200.

Though you can store the deleted value in a variable. We just want to display the status of the array after deletion.

[56, 100, 44, 76]

Note that the <span style="color:#cf2e2e" class="tadv-color">shift()</span> method is like <span style="color:#cf2e2e" class="tadv-color">pop()</span> and deletes only one element.

Rate this post