3

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.

Community
  • 1
  • 1
jason
  • 849
  • 1
  • 7
  • 17
  • 4
    I doubt if the first gives `{A : 1}`. In my console it gives an empty `[]` array (as it should be). – VisioN Jan 26 '13 at 15:13
  • @ VisioN : I am checking it in IE. In ie it is giving me above result – jason Jan 26 '13 at 15:14
  • Regardless of what the console says, `a` will have an `A` property, because arrays are objects too. – bfavaretto Jan 26 '13 at 15:15
  • Arrays are just special objects. The are handled in exactly the same way as other objects, but they treat properties with numeric names in a special way. Always use arrays only with numeric keys and everything will be alright ;) – Felix Kling Jan 26 '13 at 15:23
  • @FelixKling Numeric but not negative :) – Ja͢ck Jan 26 '13 at 16:52

5 Answers5

2

An array is also an object, that's why you can use non-numeric indices also. Those will be added as properties to the array object, not part of the array data.

The difference between an array and an object, is that the array counts properties with a numeric index as being part of the array data, and updates the length property accordingly. (Also the Array object has some specific methods to work with the array data.)

var a = [];
a[42] = 1337;

Now the length has changed to include the item:

alert(a.length); // shows 43

Using strings or numbers as index doesn't matter, a numeric index counts even if it's a string:

alert(a[42]); // shows 1337
alert(a["42"]); // shows 1337

Reducing the length removes the properties outside it:

a.length = 10;
alert(a[42]); // shows undefined
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

In your example, b and c are essentially the same thing, because {} is the equivalent of new Object().

Coming back to a, it's defined as an Array which is a special kind of Object in the sense that numeric properties (based on uint32) are treated differently. Its length property 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 how Object works with properties, so you can access it as a.A or a['A'] whereas an index of [5] can only be accessed using a[5].

Normally, the debug output of an array is always [] unless the length property 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:

(p >>> 0 === p) && (p >>> 0 !== Math.pow(2, 32) - 1)

See also:

The item exists in the array but it says that the array is 0 length?

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

Let's start by clarifying something:

new Object() is the same as {}

new Array() is the same as []

The latter are just shortened forms of the former.

Behind the scenes, everything in javascript is basically an object (this is an exaggeration but fairly accurate). Arrays are simply derived from objects. Here's a fairly rudimentary example of what an Array REALLY looks like:

var myArray = {};

myArray[0] = 'value1';
myArray[1] = 'value2';
myArray[2] = 'value3';
myArray[length] = 3;

The prototype of an array contains all the methods. For example:

// myArray#push
myArray.prototype.push = function(val){
  myArray[this.length] = val;
  this.length++;
}

Another way to illustrate this is to take an array and add keys that are not numeric:

 var example = [];

 example.push(0);
 example.push(1);
 example.push(2);

 example['a'] = 'a';
 example['b'] = 'b';
 example['c'] = 'c';

 example.log = function(){
   for(var i = 0, l = this.length; i < l; i++){
     console.log( example[i] );
   }

   console.log(this['a']);
   console.log(this['b']);
   console.log(this['c']);
 }

 // example[0] is 0
 // example[1] is 1
 // example[2] is 2

 // example.log is my custom function

 example.log(); // results in
 // 0
 // 1
 // 2
 // a
 // b
 // c

Also, don't always believe everything the console tells you. For example, if you do this:

var console_confusion = {};

console_confusion.length = 100;
console_confusion.splice = function(){ /* do absolutely nothing */ };

console.log( console_confusion );//results in
//
// in Chrome:
// [undefined × 100]
//

Chrome will interprut anything with a numeric length property and a splice function as an Array. This is why jQuery objects look like Arrays in the console.

THEtheChad
  • 2,372
  • 1
  • 16
  • 20
0

Please read the following article – http://www.2ality.com/2012/12/arrays.html

In short, “regular arrays”, denoted as [] in JavaScript, are also objects, just like {}, but they have length property and “numeric” keys (“indicies”), plus their internal __proto__ property points at Array.prototype object, which holds all Array methods, such as push or forEach etc.

Misha Reyzlin
  • 13,736
  • 4
  • 54
  • 63
0
first is an array
second is an object array
third is an array object
Rashedul.Rubel
  • 3,446
  • 25
  • 36