Flash By Night Tutorials

Multidimensional Arrays


Most programming languages, including Javascript, feature arrays. An array is a way to store a series of information. For example:

myArray=["egg","potato","beans"];

Hence, myArray[0]="egg", myArray[1]="potato", myArray[2]="beans".

Note that arrays are zero-indexed; the first value is held in myArray[0], not myArray[1].

A multidimensional array is simply an array that holds other arrays. Here is an example:

myArray=[["banana","apple","pear"],["tomato","lettuce","asparagus"],["cumin","basil","oregano"]];

 

In this case, myArray[0][0]="banana", myArray[1][0]="tomato" and myArray[2][2]="oregano".

A multidimensional array is a simple way to hold groups of data without having to involve a database. It could be used, for example, to hold questions and answers for a short quiz.