Skip to main content

Two-Dimensional Array

JavaScript doesn't support multi-dimensional arrays, but you can simulate a two-dimensional array by creating an array of arrays. The code below shows an example of creating a two dimensional array.

 
<script type="text/javascript">
<!--// 
function CreateMultiArray()
//This function creates and fills the array 
{
MultiArray = new Array(5)
MultiArray [0] = new Array(2)
MultiArray [0][0] = "Tom"
MultiArray [0][1] = "scientist"
MultiArray [1] = new Array(2)
MultiArray [1][0] = "Beryl"
MultiArray [1][1] = "engineer"
MultiArray [2] = new Array(2)
MultiArray [2][0] = "Ann"
MultiArray [2][1] = "surgeon"
MultiArray [3] = new Array(2)
MultiArray [3][0] = "Bill"
MultiArray [3][1] = "taxman"
MultiArray [4] = new Array(2)
MultiArray [4][0] = "Myrtal"
MultiArray [4][1] = "bank robber" 
}
//This function gets the array value when the form button is pressed
function getArray(row,col){
document.arrayForm.myResult.value=MultiArray[row][col];
}//end of getArray()
// End hiding. -->
</script>
 

Next: Calling the Array Function