Multidimensional Arrays (Visual Studio - JScript)

You can create multidimensional typed arrays in JScript. A multidimensional array uses more than one index to access data. When the script declares the array, it sets the range for each index. Multidimensional arrays are similar to arrays of arrays, where each subarray can have a different length. For more information, see Arrays of Arrays.


The data type name followed by a pair of square brackets ([]) specifies a one-dimension array data type. The same procedure specifies a multidimensional array data type, but commas (,) are between the brackets. The dimensionality of the array is equal to the number of commas plus one. The following example illustrates the difference between defined a one-dimensional array and a multidimensional array.

// Define a one-dimensional array of integers. No commas are used.
var oneDim : int[];
// Define a three-dimensional array of integers.
// Two commas are used to produce a three dimensional array.
var threeDim : int[,,];

In the following example, a two-dimensional array of characters is used to store the state of a tic-tac-toe board.

// Declare a variable to store two-dimensional game board.
var gameboard : char[,];
// Create a three-by-three array.
gameboard = new char[3,3];
// Initialize the board.
for(var i=0; i<3; i++)
   for(var j=0; j<3; j++)
      gameboard[i,j] = " ";
// Simulate a game. 'X' goes first.
gameboard[1,1] = "X"; // center
gameboard[0,0] = "O"; // upper-left
gameboard[1,0] = "X"; // center-left
gameboard[2,2] = "O"; // lower-right
gameboard[1,2] = "X"; // center-right, 'X" wins!
// Display the board.
var str : String;
for(var i=0; i<3; i++) {
   str = "";
   for(var j=0; j<3; j++) {
      if(j!=0) str += "|";
      str += gameboard[i,j];

The output of this program is:

O| | 
 | |O

You can use a multidimensional typed array of type Object to store data of any type.

See Also


Array Data

Arrays of Arrays

Other Resources

JScript Arrays