JS: shift() Method

Advertisements
Advertisements

The pop() 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 shift() method.

In this example, we will create an array and use the shift() method to remove an element from the front.

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

In the above example, the shift() method will remove the first element from the front which is 200.

Advertisements

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 shift() method is like pop() and deletes only one element.

Advertisements
Advertisements
Advertisements