js add element to start 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: js add element to front of array

//Add element to front of array
var numbers = ["2", "3", "4", "5"];
numbers.unshift("1");
//Result - numbers: ["1", "2", "3", "4", "5"]

Example 3: unshift method in javascript

var name = [ "john" ];
name.unshift( "charlie" );
name.unshift( "joseph", "Jane" );
console.log(name);

//Output will be
[" joseph "," Jane ", " charlie ", " john "]