JavaScript

June 6, 2023

How to use JavaScript Array shift()

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

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

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

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

by : Suhel Akhtar

Quick Summary:

JavaScript Array shift() method