Multi-Dimensional Arrays |
JScript's internal array creation mechanism only supports single-dimension arrays. Therefore you can declare an array of 5 elements (x=new Array(5)), but you cannot directly declare a 2D array. To circumvent this restriction, JScript allows "nested" arrays to achieve multiple dimensions. An example of this would be the following code which creates a 2 x 2 2D array:
var js2DArray = new Array(2);
for (var j = 0; j < js2DArray.length; j++)
{
// Prepare to fill it with rows.
var aRow = new Array(2); // Create a row.
for (var i = 0; i < aRow.length; i++)
{
// fill the row with zeroes.
aRow[i] = 0;
}
// Put the row into the outer array.
js2DArray[j] = aRow;
}
Once a nested array is declared, elements in the array can be accessed using multiple sets of brackets ([]). For example, js2DArray[col][row] is valid for the example above, while js2DArray[col,row] would generate an error.