Possible Duplicate:
What is the difference between an array and an object?
The item exists in the array but it says that the array is 0 length?
I m a bit confused in object and associative array in javascript. I read this :question but this question says that there is not much of a difference in both. I wrote this in the console:
var a = [];
a["A"] = 1;
var b = {};
b["B"] = 2;
var c = new Object();
c["C"] = 3;
output for above are as:
a gives {A : 1}
b gives {B : 2}
c gives {C : 3}
All above three cases gives same reault as in they all gives an object. Question is how all above 3 cases are treated in javascript.
In your example,
bandcare essentially the same thing, because{}is the equivalent ofnew Object().Coming back to
a, it’s defined as anArraywhich is a special kind ofObjectin the sense that numeric properties (based on uint32) are treated differently. Itslengthproperty gets updated when those properties are added and removed.When you use
'A'as an index, it gets treated as a regular property, defined by howObjectworks with properties, so you can access it asa.Aora['A']whereas an index of[5]can only be accessed usinga[5].Normally, the debug output of an array is always
[]unless thelengthproperty is non-zero, so the output you’ve shown is somewhat irregular.Trivia
According to the ECMAScript documentation, a particular value p can only be an array index if and only if:
See also:
The item exists in the array but it says that the array is 0 length?