Creating a JavaScript Array

An array can be created in JavaScript by any of the following:

var aryTest = new Array();
This initializes an empty array

var aryTest = new Array(2);
This initializes an array with the number of elements indicated in the parenthesis. Each element is undefined, which is demonstrated by looping over the array as follows:
for(i=0;i<aryTest.length;i++)
alert(aryTest[i]);

var aryTest = new Array(1,2,3,4);
This initializes an array while populating it with values. Therefore, the length of this array would be four with 1 being the value of the first array element, 2 the value of the second array element and so forth.

Leave a Reply