How can I create a blank array of length 4 in jQuery. I want to create the array first and later want to punch values in it.
1 Answer
Use Array Constructor, new Array(arrayLength)
var array = new Array(4);
Note: This array is not essentially blank, it contains undefined but it's length is 4.
If feasible,
var arr = [1, 2, 3, 4];
2 Comments
Akki619
and also with var array = ["1","2"] with default values. console.log(array.length)
Tushar
@Akki619 I've updated answer, but it is not always possible, when you have large number of elements and you want to create array dynamically, instead of loop,
Array constructor is better choice.