Using the Java Script Array Object
By Stephen Bucaro
An array is a structure for storing multiple data values. Each value in an array
is given an index number. To access a particular data item in an array, use the name
of the array and the index number of the particular data item.
Creating an Array
To create an Array, use new keyword. The code shown below creates an array
named color that can hold three data items.
var color = new Array(3);
Putting Data into an Array
To enter data into an array, create a series of assignment statements, one for each
data item in the array. The code shown below populates the color array with the
names of three colors.
color[0] = "red";
color[1] = "green";
color[3] = "blue";
Note that array indexes are zero-based, in other words the index values start with
0 for the first element.
The new keyword is required only when creating an array with object
type data. In the previous example we created an array of string objects. The
new keyword is not required when creating an array of simple variable types.
The code shown below creates and populates an array with integers.
var digit = [1, 2 ,3];
The code shown below creates and populates an array with characters.
var char = ['a', 'b' ,'c'];
Accessing Array Values
To access a particular data item in an array, use the item's index number. The code
shown below displays the value of the second data item in the array of characters created above.
alert(char[1]);
Output:
b
To access the entire contents of an array, use the name of the array without an index number.
The code shown below displays the value of all the data items in the array of characters.
alert(char);
Output:
a,b,c
|