Open In App

Node.js shift() function

shift() is an array function from Node.js that is used to delete elements from the front of an array. 

Syntax:



array_name.shift()

Parameter: This function does not take any parameter. 

Return type: The function returns the array after deleting the element. 



The program below demonstrates the working of the function: 

Program 1: 




function shiftDemo() {
    arr.shift();
    console.log(arr);
}
const arr = [17, 55, 87, 49, 78];
shiftDemo();

Output:

[ 55, 87, 49, 78 ]

Program 2: 




function shiftDemo() {
    arr.shift();
    console.log(arr);
}
const arr = ['a', 'b'];
shiftDemo();

Output:

[ 'b' ]

Program 3: 




const Lang = ["Python", "C", "Java", "JavaScript"];
while ((i = Lang.shift()) !== undefined) {
    Lang.shift();
}
console.log(Lang);

Output:

[]

Article Tags :