0

I am aware that everything is an object in JavaScript but something struck me when i was using the console of Internet Explorer -F12 (Yes IE, not allowed to use other browsers)

If i type a sample array in the console as:

[1,2]

the output is

1,2{
    0:1,
    1:2
   }

Does this mean that JavaScript converts Array into an Object with keys and values?

krishna
  • 575
  • 2
  • 8
  • 22

1 Answers1

3

Yes, arrays in JS are simply objects with numeric keys. You could do the reverse:

var myarray = { 0: 'first', 1: 'second', 2: 'third' };
console.log(myarray[1]);
Matt Pileggi
  • 7,126
  • 4
  • 16
  • 18
  • 1
    for some reason that "array"s length property is wrong and [].join doesn't work either... Maybe it's not-so-simple? – dandavis Feb 25 '14 at 17:17
  • Well, Array is a special object so my example does not inherit the Array.prototype. So I guess in that sense it's not the exact same thing. But the OP was asking if the data itself is stored the same way. – Matt Pileggi Feb 25 '14 at 17:18
  • if the question is is " the data itself is stored the same way", the answer is still no. V8 for example would store the OP's code as an intArray, but an object "equivalent" uses a different data structure altogether. – dandavis Feb 25 '14 at 17:19