HTML and CSS Reference
In-Depth Information
of 2 (because month values start with 0 for the month of January) and a four-digit year
value of 2015. However, Maria wants the month name rather than the month number to
appear in the table, but no existing date method returns the name of the month. Instead,
you'll have to write code to associate each month number with a month name. One way
of doing this is by using an array.
An array is a collection of values organized under a single name. Each individual
value is associated with a number known as an index that distinguishes it from other
values in the array. Array values are referenced using the expression
array [ i ]
where array is the name of the array and i is the index of a specific value in the array.
Index values start with 0, so that the initial item in an array has an index value of 0, the
second item has an index value of 1, and so on. For example, the expression
references the fifth (not the fourth) item in the monthName array.
Creating and Populating an Array
To create an array, you can apply the object constructor
A common programming
mistake with arrays is to
use parenthesis symbols
() rather than square
brackets [] to create and
reference array values.
Remember that only
square brackets should
be used.
var array = new Array( length );
where array is the name of the array and length is the number of items in the array.
The length parameter is optional; if you omit this parameter, the array expands auto-
matically as more items are added to it. However, when you define the length of an array
yourself, JavaScript allots only the amount of memory needed to generate the array, so
the code runs more efficiently. Thus, to create an array named monthName for the
12 month names, you'd enter the following statement:
var monthName = new Array(12);
Alternatively, you could omit the array length and enter the statement as follows:
var monthName = new Array();
Once you've created an array, you can populate it with values using the same commands you
use for any variable. The only difference is that you must specify both the array name and the
index number of the array item. The command to set the value of a specific item in an array is
array [ i ] = value ;
where array is the name of the array, i is the index of the array item, and value is the
value assigned to the item. For example, to insert the month names into the monthName
array, you could run the following commands:
monthName[0] = “January”;
monthName[1] = “February”;
monthName[2] = “March”;
. . .
monthName[10] = “November”;
monthName[11] = “December”;
A more compact way of creating and populating an array is to specify the array values
when the array is initially declared using the statement
var array = new Array( values );
where values is a comma-separated list of values. Thus, the following command both
creates and populates the monthName array within a single statement:
var monthName = new Array(“January”, “February”, “March”, “April”,
“May”, “June”, “July”, “August”, “September”, “October”, “November”,
Search WWH ::

Custom Search