+ 1
How can I add new array elements at the beginning of an array in Javascript?
2 Answers
+ 2
Something like this?
var arr = [];
arr.unshift(1)
arr.unshift(2)
arr.unshift(3)
console.log(arr)
> [3, 2, 1]
0
var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";
console.log(arr.join());
arr.splice(2, 0, "Lene");
console.log(arr.join());
The output of the above code will be something like this,
//Output
Jani,Hege,Stale,Kai Jim,Borge
Jani,Hege,Lene,Stale,Kai Jim,Borge