How to use JavaScript Array shift()

Array shift

Array shift() method

Program to use JavaScript Array shift() method

let a = [1,2,3,4,5];
    let b = a.shift();
    
    console.log(a);
    console.log(b);
    

Output

[ 2, 3, 4, 5 ]
    1
    

Steps Description

  • Declare two variable a and b
  • Initialize two variable with value of array a = [1,2,3,4,5] and b = a.shift()
  • The array shift() method removes the first element of an array.
  • The value of b variable is displayed with the help of console.log inbuilt function

Array shift() method

Program to use JavaScript Array shift() method

let c = ['Good Morning', 'Good Afternoon', 'Good Evening', 'Good Night'];
    let d = c.shift();
    
    console.log(c);
    console.log(d);
    

Output

[ 'Good Afternoon', 'Good Evening', 'Good Night' ]
    Good Morning
    

Steps Description

  • Declare two variable c and d
  • Initialize two variable with value of array c = [‘Good Morning’, ‘Good Afternoon’, ‘Good Evening’, ‘Good Night’] and d = c.shift()
  • The array shift() method removes the first element of an array.
  • The value of d variable is displayed with the help of console.log inbuilt function