O'Reilly logo

The Book of JavaScript, 2nd Edition by thau!

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Associative Arrays

All the arrays we've seen so far have stored values according to their numerical position in the array. An associative array uses strings instead of numbers to store values. For example, the following lines create a phone book with an associative array:

var phone_book = new Array();
phone_book["dave thau"] = "(415) 555-5555";
phone_book["information"] = "(415) 555-1212";

The first line creates a new, empty array, as we've seen before. The next two lines put two associations into the array. The first associates the string dave thau with another string, (415) 555-5555. The second associates the string information with the number to dial for Information. To retrieve that number, you would look it up using a line like this:

var information_number ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required