JavaScript Shift() method

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: