An array allows you to store a list of common elements in a variable as shown in the following example:
var models = new Array("Ford", "Mazda", "Honda");
You can easily access the elements of an array by using the index number assigned to each element. Elements are stored in sequential order beginning with index number 0, proceeding with index number 1, and so on. Since the index numbering begins with 0, the array's item count will always be one higher than the highest value of the array. The element's index number is enclosed in square brackets and constitutes its location in the array. The Array is a core object.
To set the first element of the array in the example shown above, you would type:
models[0];
When you execute the JavaScript, the variable will contain the "Ford" string.
arrayLength
(Optional) The initial length of the array. You can access this value using the length property.
element
(Optional) A list of values for the array's elements. When this form is specified, the array is initialized with the specified values as its elements, and the array's length property is set to the number of arguments.
An array's length increases if you assign a value to an element higher than the current length of the array. The following code creates an array of length 0, then assigns a value to element 99. This changes the length of the array to 100.
You can construct a dense array of two or more elements starting with index 0 if you define initial values for all elements. A dense array is one in which each element has a value. The following code creates a dense array with three elements:
myArray = new Array("Hello", myVar, 3.14159)
The result of a match between a regular expression and a string can create an array. This array has properties and elements that provide information about the match. An array is the return value of RegExp.exec, String.match, and String.replace.
To help explain these properties and elements, look at the following example and then refer to the table below:
//Match one d followed by one or more b's followed by one d //Remember matched b's and the following d //Ignore case myRe=/d(b+)(d)/i; myArray = myRe.exec("cdbBdbsbz");
The following table lists the properties and elements returned from this match.
The following example creates an array, msgArray, with a length of 0, then assigns values to msgArray[0] and msgArray[99], changing the length of the array to 100.
msgArray = new Array() msgArray [0] = "Hello" msgArray [99] = "world" // The following statement is true, // because defined msgArray [99] element. if (msgArray.length == 100) Console.Write("The length is 100.")
The following code creates a two-dimensional array and displays the results.