JavaScript array unshift() method

The unshift() method adds one or more items to the beginning of an array and returns the new length of the array. This can be useful for adding items to the beginning of an array, or for combining multiple arrays together.

// create an array with some items
const colors = ["red", "green", "blue"];

// print the array
console.log(colors);  // Output: ["red", "green", "blue"]

// add a new item to the beginning of the array
colors.unshift("yellow");

// print the array
console.log(colors);  // Output: ["yellow", "red", "green", "blue"]

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 )

Facebook photo

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

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: