+ 8
How to create empty array in JavaScript and insert the values after
4 Answers
+ 8
// You can do this by:
var arr = [] ;
arr.push("value1");
arr.push("value2");
//...
console.log(arr);
// value1, value2
+ 2
You can create an empty array as var arrayName= [];
and then to insert value in array you have many choices as
1. Using for loop
for(var i= 0; i<noOfElements; i++){
arrayName.push("value");
}
2. Using this method as
arrName[0]= value1;
arrName[1]= value2;
etc
+ 1
var x = [];
x[0] = 1;
x[1] = 2;
alert(x);