JavaScript Array shift() method

Learn about JavaScript Array shift() method

Posted on February 08, 2021


The JavaScript Array shift() method allows you to remove the first element (at index zero) of your array, while also returning that element to you. The shift() method does not have any parameters.

Here’s an example of shift() in action:

let programmingLangs = ["JavaScript", "Ruby", "Python"];
let shiftedLang = programmingLangs.shift();

console.log(programmingLangs); // ["Ruby", "Python"]
console.log(shiftedLang); // "JavaScript"

Because the first element is a string, the value returned by shift() is also a string. If your array contains another array or an object, that will be returned as well:

let fruitCart = [
  {
    name: "Apple",
    quantity: 3,
  },
  {
    name: "Orange",
    quantity: 1,
  },
  {
    name: "Banana",
    quantity: 3,
  },
];

let shiftedFruit = fruitCart.shift();
console.log(shiftedFruit); // { name: "Apple", quantity: 3 }

You can use the shift() method for any array type data you have.

See also: JavaScript methods to remove element from Array

Related articles:

Grab the free JavaScript book today 👍

Learn the building blocks of JavaScript programming language like data types, functions, objects, arrays and classes.

Use the knowledge from the book to build a small but solid program.

Learn more