Arrays can be thought of a list or set of multiple values. They can be of string or numerical format and, have no maximum boundaries. The syntax used are parenthesis that hold the array set and each item is then comma delimited. They are voltile, meaning that thevalues are not saved after the page containing the script has closed or reloaded.
Single (One) Dimensioned Arrays
A example of a single-dimensioned array is the following:
myArray = new Array(23, 19, 8, 41, 55) (which in this case is a set of people’s ages).
This expression states – create an array called ‘myArray’ with five values. The values are 23,19,8,41,55. Each item is represented by an index, starting with 0, where index 0 = item 23, index 1 = 19, index 2 = 8 and so on.
The manner (format) in which we address specific items in an array is:
myArray[x], where ‘x’ represents the item placement in the array.
myArray would address the array item 41.
An important property of the array object is the length property; the basic numerical array above has a length of five. Each of the values in the array can be accessed using their index within square brackets following the array name, for example:
It’s important to reiterite that the indices of arrays always begin with 0, representing the first item in the array; therefore, while the length property of this array is five, the highest index is four.
To make sense out of calling array items, we just add a function. For example, it we were to tell the printer to print array item 4, we would using the document.write function as follows:
document.write(myArray), which simply display the item ‘41’; the 4th item in the set.
Another example is using arrays in math equations:
var mySum = myArray * myArray
which would display the number 437.