Javascript Programming
Fundamental Data Types
ARRAY
An array is a collection of data. Any time you want to organize information into a list of some sort, an array is the way to go. An array is created using the following syntax:
var myArray = new Object();
This variable "myArray" can now be used to store values...
myArray[0] = new String('Hello
')
myArray[1] = new String('World! ')
myArray[2] = new String('Michael Curry')
Now, writing the contents of the array into an alert is as easy as this:
alert(myArray[0] + myArray[1] + myArray[2]); |
Arrays are especially useful when you have large amounts of data to display. For instance, if you were writing a shopping cart and wanted to total all of the items, you might have an array that looked something like this:
myArray[0] = new
Number(55.50);
myArray[1] = new Number(22.75);
myArray[2] = new Number(88.50);
myArray[3] = new Number(72.23);
myArray[4] = new Number(48.71);
myArray[5] = new Number(105.57);
Instead of adding each of these numbers one at a time, it would be so much easier to loop through like so:
var subTotal =
new Number(0); |
Now the variable subTotal holds the total cost, and you
totaled the value in just 3 lines!