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