In the construction of a dense array
, the array constructor and all the array's elements are contained on one line. Its elements are represented only by their data (no array name, brackets, or indices), and serve as parameters to the Array function. Each element is comma delimited, their indices now assumed chronologically.
var files = new Array("1st.htm", "2nd.htm", "3rd.htm", "4th.htm")
The elements of both long and dense arrays are accessed the same way:
Parallel arrays are 2 or more arrays whose corresponding element indexes contain related information. An example of a parallel array could be an array holding the names of directories that contain the files stored in the myScripts array.
var myFolder = new Array()
myFolder = "docWrite/"
myFolder = "rollover/"
myFolder = "functions/"
myFolder = "location/"
var myScripts = new Array()
myScripts = "documentWrite.html"
myScripts = "simpleRollover.html"
myScripts = "bgColorFunction.html"
myScripts = "location.html"
The myFolder and myScripts arrays are parallel because their indices contain related information. Used together, the myFolder and myScripts arrays could be used to change the URL:
location.href = myFolder + myScripts
Multi-Dimensional Arrays are arrays whose elements are themselves arrays. You can build a multi-dimensional array as deep as you want, but they can be as unreadable and confusing as they are useful. Take a look at a piece of the multi-dimensional array used to generate the links on this site:
var week = new Array()
week= new Array("TUTORIAL 1",
new Array("How To Use This Site",
new Array("Objects, Properties and Methods","jsIntro2.html"),
new Array("Two Parts to Scripting, The Dot Syntax","jsIntro3.html")
A simple reference to the topmost array is simply week
A reference to a part of the topmost array beginning at a second level array (there are 3 second level arrays in this example because week contains 3 elements which happen to be arrays) requires another set of square braces containing the index of the desired array: week
Specifying a bit of information beginning at a third level array requires another set of square braces containing the desired index: week
. Zeroing in even further: week
You can even specify a single character in the string literal value of a nested array because a string literal itself is considered an array, and each character of the literal an element of it (we will discuss strings in depth later on): week.charAt(37)
Although multidimensional arrays provide great organization for a lot of related information, simple arrays are easier to work with.