Electric Type

Multimedia

About Us

News

Help

Advanced JavaScript Tutorial
Lesson 2

by Thau!

Page 5 — Associative Arrays

If you've done any JavaScript programming, you're probably familiar with arrays, which let you store lists of items and also give you access to the images, forms, and form elements of your HTML pages.
    var an_array = new Array("hickory","dickory");
    
    var element_one = an_array[0];
    
    var element_two = an_array[1];
    
    an_array[2] = "doc";
    

This creates a new array and initializes it with two strings. The first element of the array is accessed using its index number, "0." The second element, element 1 of the array, is accessible with an_array[1]. You can add to an array by assigning something to a specific index of the array: in the example above, I made the third element of the array equal to "doc." Now the array contains "hickory, dickory, doc."

Associative arrays are just like the array above, except rather than using numbers to index elements in the array, you use words.

    var phone_book = new Array();
    
    phone_book["sleepy"] = "(203) 555-1234";
    
    phone_book["happy"] = "(203) 555-2345";
    
This creates sort of a phone book. Access the phone number for "happy" by writing:
    var happy_number = phone_book["happy"];
    

Try out the working phone book example on the next page to see how an associative array might work (and get a major refresher on the use of JavaScript with forms at the same time).

next page»


Tutorials  

User Blogs  

Teaching Tools  

Authoring  

Design  

Programming help  

Advanced Flash  

Javascript  

Glossary  

PHP Coding  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.