add to the beginning of an array javascript code example
Example 1: javascript add new array element to start of array
var colors = ["white","blue"];
colors.unshift("red");
Example 2: unshift method in javascript
var name = [ "john" ];
name.unshift( "charlie" );
name.unshift( "joseph", "Jane" );
console.log(name);
[" joseph "," Jane ", " charlie ", " john "]
Example 3: insert element at beginning of array javascript
const array = [3, 2, 1]
const newFirstElement = 4
const newArray = [newFirstElement].concat(array)
console.log(newArray);
Example 4: javascript move array element to front
for(var i = 0; i<$scope.notes.length;i++){
if($scope.notes[i].is_important){
var imortant_note = $scope.notes.splice(i,1);
$scope.notes.unshift(imortant_note[0]);
}
}
Example 5: javascript append how first element
var eElement;
var newFirstElement;
eElement.insertBefore(newFirstElement, eElement.firstChild);