The array shift() is a built-in JavaScript function that removes the first element from the array and returns that deleted item. The syntax for shift() is array.shift(), which does not accept any parameters and changes the array’s length.
Syntax
array.shift()
An array element can be a string, a number, an array, a boolean, or any other object allowed in the Javascript array.
Example 1: How to use the array.shift() method
let apps = ["Instagram", "Facebook", "Messanger"];
apps.shift();
console.log(apps);
The first item of the array will be removed, and the array’s length will be 2.
Example 2: Using the shift() method in while loop
To check a specific condition using the shift() function, we can use a while loop. For example, we can use the shift() method in a while loop like the below code.
let names = ["Harvey", "Donna", "Mike", "Rachel" ,"Louis", "Jessica"];
while( (i = names.shift()) !== undefined ) {
console.log(i);
}
So until the value of undefined is found, it will remove each element of an array.
The shift() method deletes and returns the removed element of the original array.
All remaining elements in the array get shifted one slot to the left to fill the hole created by removing the first element. It will not create a new array.
The JavaScript shift() method removes the first element, whereas the pop() method removes the last element of an array.
It works really well for me
Suits!!
nice post