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?

5
  • 1
    Array's are object, so yes or no depending on how you look at it. Commented Feb 25, 2014 at 17:14
  • Is this really exactly the output you're getting? It doesn't even make any sense Commented Feb 25, 2014 at 17:14
  • the output shown is merely a visualization by a graphical debug console... you can log JSON.stringify(arr) if in doubt... Commented Feb 25, 2014 at 17:15
  • @dandavis The third comma was bothering me. Commented Feb 25, 2014 at 17:16
  • typeof [1,2,3] results in "object" so there is no primitive object type Commented Feb 25, 2014 at 17:16

1 Answer 1

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]);
Sign up to request clarification or add additional context in comments.

3 Comments

for some reason that "array"s length property is wrong and [].join doesn't work either... Maybe it's not-so-simple?
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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.