1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Associative Arrays

clr gif
Join the Discussion

Questions? Comments?

An associative array differs from other arrays in that the entries in the array are referenced by name rather than by their location in the array. Instead of having myarray[0], myarray[1], etc. we instead have myarray['page'], myarray['section'], etc. where the names of each entry are meaningful descriptions of what each entry represents.

With an associative array the individual entries can contain quite different contents so that just because one entry in the array is a number doesn't mean that another entry in the array can't be a text string. Of course you could have different types of entries in a regular numbered array just as easily but since the numbering of the entries implies an order to those entries it is more natural to use numbered arrays for multiple occurrences of similar items and associative arrays where the items are different.

An associative array has no particular ordering as such, neither does it have a length property to tell you how many entries there are in the array. You can't process all of the entries in an associative array using:

for (var i = myarray.length; i > 0; i--) {

This method of coding not only requires that the array have a specific length (which an associative array does not have) but also requires that the entries have a particular order (in this case descending) which the entries in an associative array also do not have. Instead, if you want to process all of the entries in an associative array you need to use the in keyword with coding like:

for (ent in myarray) {

This code allows all of the entries in an associative array to be processed without requiring any knowledge of the number of entries in the array and without specifying any particular order in which the entries are meant to be processed - since the entries within an associative array don't have any particular order in which the entries need to be processed.

See More About

©2016 About.com. All rights reserved.