Credits
Help with JavaScript
Thanks to Brenden Eich of Netscape for explaining to me how to create the original JavaScript Arrays back when the language was known as Livescript. You can find further documentation of JavaScript arrays in the Netscape documentation:
Please note that Javascript arrays have changed drastically between Netscape 3 and 4 (Communicator). If you are using communicator, you can still use the old syntax if you do NOT specify "language=Javascript 1.2" in your script tag or if you specify a lesser version of the language.
| Section |
Summary |
| Javascript arrays are called "associative arrays". Learn what this term means in this section. |
|
| Array creation is less simple in Javascript than in some other languages. In this section, learn how to create a new array. |
|
| Once an array is created, its elements must be filled. Filling an array is discussed in this section. |
|
| Values in Javascript arrays can be accessed in more than one way. Learn how to do that here. |
|
| Theory is nice but let's see some action. This example stores the hexadecimal values of the eight primary colors in an array. It looks them up upon choice from a list. |
|
Associative arrays are X by 2 arrays; i.e, two dimensional arrays where the first dimension can be unlimited in size but the second dimension has a size of two. They consist of a list of "words" and "associated word" pairs. This might be translated into pairing types such as "keywords" and "definitions", "left" and "right" objects, or "number" and "meaning". Associative arrays should be familiar to users of Scheme and Lisp as "car" and "cat". VBScript calls this a dictionary. As a first example, let us examine a list of colors and their hexadecimal equivalents:
| black |
#000000 |
| red |
#FF0000 |
| green |
#00FF00 |
| blue |
#0000FF |
| magenta |
#FF00FF |
| yellow |
#FFFF00 |
| cyan |
#00FFFF |
| white |
#000000 |
For every item in the left column, there is an associated item in the right column. This array has eight pairs. That is, it has a size of 8.
In Netscape 2.x, the first element (array[0]) of an associative array contained its size. Consequently, it we defined an array named hexColors, element 0 would contain "8", and elements 1-8 would contain the eight pairs which we just specified. Javascript arrays are also ordered. That is, if you could retrieve hexColor[2] and always obtain the same result. Current versions of Javascript/JScript now define an array property of length so you can get the size of the array with array.length. Note that Javascript arrays are zerobased. The colhex array has a length of nine. Interesting enough, although the 4.x browsers have no problem dealing with the old method of producing an array, they no longer put the length in 0. The way to accomodate old browsers is to put the length in the 0 element yourself.