javascript add item on top of array code example
Example 1: javascript add new array element to start of array
var colors = ["white","blue"];
colors.unshift("red"); //add red to beginning of colors
// colors = ["red","white","blue"]
Example 2: javascript unshift
let array = ["A", "B"];
let variable = "what you want to add";
//Add the variable to the beginning of the array
array.unshift(variable);
//===========================
console.log(array);
//output =>
//["what you want to add" ,"A", "B"]
Example 3: how to push at top of array
const array = [3, 2, 1]
const newFirstElement = 4
const newArray = [newFirstElement].concat(array) // [ 4, 3, 2, 1 ]
console.log(newArray);