Associative Arrays part 1

JavaScript Tutorial
Core Objects
Associative Arrays
46
1/2
         

Associative Arrays



While many programming languages support arrays with named indexes (text instead of numbers), called associative arrays,JavaScript does not.
However, you still can use the named arraysyntax, which will produce an object.
For example:
var person = []; //empty array
person["name"] = "John";
person["age"] = 46;
document.write(person["age"]);
//Outputs "46"
Try It Yourself
Now, person is treated as an object, instead of being an array.
The named indexes "name" and "age" become properties of the person object.
As the person array is treated as an object, the standard array methods and properties will produce incorrect results. For example, person.length will return 0.

Comments

Popular posts from this blog

String Operators

Introducing object part 3

Associative Arrays part 2