In the same way that arrays contain integers or strings as values, multidimensional arrays contain arrays as their values. The syntax for a multidimensional array is as follows:
myArray = new Array([2,3,4], [5,5,9], [8,6,1])
To display the first item of the first array (which has an index of 0,0) you would need to use:
This is known as a two-dimensional array, even though there are three arrays each with three values, as the highest index is 2,2. Referencing each of the items in multidimensional arrays can be cumbersome and confusing, and I have found it easiest to keep arrays singular and just have more of them as necessary, i.e. have three separate arrays rather than one two-dimensional array.
Finding Uses for Arrays
To show you how useful arrays are when you need to store and retrieve information, I have put together an example that demonstrates their power and usefulness. Imagine that you have an online store selling computer components, and you offer five products out of five of your product lines as special offers every week. Obviously there is only so much information you can fit onto your homepage, so you can’t show all 25 of the week’s special offers at once. But because you want to alert people that these offers exist, you need something on the homepage.
You decide that a random image generator would be your best use of space, and you want it to display one offer from each of the product lines at a time. You want each of the images to be a roll-over, the staple of any wholesome website. Each of the images should also link to a new page, giving the full description and details of each of the products. All of a sudden there is an incredible amount of information that your script is going to need to use; an array is going to be needed to keep track of it -- several arrays in fact.
This is a basic example of how you might put together the special offers box:
We’ll put the script in its own .js file for simplicity, so open up your favorite text editor and move on to the next page.