1var colors = ["white","blue"];
2colors.unshift("red"); //add red to beginning of colors
3// colors = ["red","white","blue"]
1// Build an array of test data.
2var data = [ "X" ];
3
4// Unshift data onto the array. Unshift() prepends elements to
5// the beginning of the given array. Note that it can take more
6// than one argument. In the output, notice that when unshift()
7// takes multiple arguments, they are prepended in a right-to-left
8// order (mimicing their appearence in the arguments list).
9data.unshift( "A" );
10data.unshift( "B", "C" );
11
12// Output resultant array.
13console.log( data );
1 let array = ["A", "B"];
2 let variable = "what you want to add";
3
4//Add the variable to the beginning of the array
5 array.unshift(variable);
6
7//===========================
8 console.log(array);
9//output =>
10//["what you want to add" ,"A", "B"]
1var a = [23, 45, 12, 67];
2a.unshift(34);
3console.log(a); // [34, 23, 45, 12, 67]