HTML and CSS Reference
In-Depth Information
“#FF0000”
“#0000FF”
“#00FF00”
hex[“red”]
hex[“blue”]
hex[“green”]
Figure 9.2 An associative Array object called hex . Index values are strings.
9.2.1 Declaring and Populating Arrays
Like variables, arrays must be declared
before they can be used. Let's examine
some ways to create an array.
Creating an Array Object with
new . The new keyword is used to
dynamically create the Array object. It
calls the Array object's constructor func-
tion, Array() , to create a new Array
object. The size of the array can be
passed as an argument to the constructor,
but it is not necessary. Values can also be
assigned to the array when it is con-
structed, called a dense array, but this is
not required either. The following array
is called array_name and its size is not
specified.
FORMAT
var array_name = new Array();
Example:
var months = new Array();
months[0]="January";
months[1]="February";
months[2]="March"
In the next example, the size or length of the array is passed as an argument to the
Array() constructor. The new array has 100 undefined elements.
var array_name = new Array(100);
And in the following example, a dense array is given a list of initial values at the same
time it is being declared:
var weekday = new Array("Sunday", "Monday", "Tuesday");
 
 
Search WWH ::




Custom Search