HTML and CSS Reference
In-Depth Information
An Array object is declared.
The length property is used to get the length of the array. The length is 6.
The book is an Array object created by the Array() constructor. The constructor
property shows that this is native (primitive) code and the name of the construc-
tor function is Array() .
The book array object is set to a length of 0, meaning all elements with an index
of 0 or more will be removed from the array, leaving an empty array. If you set the
length to 2, all values with index value of 2 or more will be removed.
The value is undefined. It was removed by setting the length of the array to 0.
All the elements of the array have been removed; its length is now 0. Figure 9.7
shows the output.
Figure 9.7 Array properties.
9.2.3 Associative Arrays
JavaScript allows use of a string value as an index of an array. An array that uses a string
as an index value instead of a number is called an associative array. There is an associa-
tion between the index and the value stored at that location. The index is often called a
key and the value assigned to it, the value . Key/value pairs are a common way of storing
and accessing data. In the following array called states , there is an association between
the value of the index, the abbreviation for a state (e.g., “CA” ), and the value stored
there—the name of the state (e.g., “California” ). The special for loop can also be used to
iterate through the elements of an associative array. We will see many examples through-
out the rest of the text where associative arrays are used. 1
1. This type of array has nothing to do with the built-in Array object. Saying object[“property”] is just
another way of saying It's another way of representing an object's property. This means
that the Array object's length property is not used with associative arrays, nor do the Array methods apply,
which are covered later in this chapter.
Search WWH ::

Custom Search