HTML and CSS Reference
In-Depth Information
ARRAYS
A variable can have only a single value at one time. h e value can be a computed based on a
combination of dif erent values, but once it's stored inside a variable, it becomes one. For
instance, as shown in the previous section on variables,
firstNumber = 123 ;
secondNumber = 7 ;
total = firstNumber + secondNumber ;
h e variable named total is the sum of the i rst two variables. It is a single entity. h is would
be true were they concatenated as well. So, just remember: Variables can have only one value
at a time. Figure 12-5 provides a graphic illustration of the dif erence between variables and
arrays.
Variable
Array
TrueLove
stuff[0]
stuff[1]
stuff[2]
stuff[3]
248
Variable name:
TrueLove
Array name:
stuff
Figure 12-5: Storing data in variables and arrays.
As you can see in Figure 12-5, only a single item is stored in the variable named TrueLove ,
but the array, stuff , has lots of, well, stuf . You call each one of the stored datum with the
array name with a number in braces. So, stuff[1] is an anchor and stuff[2] is a
calculator.
Some applications require multiple values in a single object, making it easier to recall and
store data. Each value in an array is called an element. You reference each by a number,
beginning with zero (0) and numbered sequentially (refer to Figure 12-5). Suppose you have
an array named fruit . You might assign values as shown here:
<! DOCTYPE HTML >
< html >
< head >
< script type = ”text/javascript” >
fruit = new Array();
fruit [ 0 ]= ”rasberries” ;
fruit [ 1 ]= ”peaches” ;
fruit [ 2 ]= ”apples” ;
 
Search WWH ::




Custom Search