The shift()
method removes the first item in an array and returns the removed value. This can be useful for removing items from the beginning of an array, or for getting the first item in an array without modifying the array itself.
// create an array with some items
const colors = ["red", "green", "blue"];
// print the array
console.log(colors); // Output: ["red", "green", "blue"]
// remove the first item in the array
const firstColor = colors.shift();
// print the array
console.log(colors); // Output: ["green", "blue"]
// print the value that was removed
console.log(firstColor); // Output: "red"
Leave a Reply