4

If I execute below code on chrome console then, I got answer as associative array:

var arr= [];
var i = 1;
for(var j = 1; j < 3; j++)
    arr[j]=j;console.log(arr);

Ans : [1: 1, 2: 2]

But when i execute using node: [ , 1, 2 ]

Why there is so difference? As far as i know both are using v8.

1
  • why associative? its a normal array Commented Aug 13, 2015 at 7:03

1 Answer 1

5

Firefox says

Array [ <1 empty slot>, 1, 2 ]

IE Edge says

[object Array][undefined, 1, 2]

and, they're all correct

Chrome is simply NOT reporting the empty index 0

Node is showing index 0 is empty

Firefox is telling you exactly what's happening

Try this:

var arr= [];var i = 1; for(var j = 1; j < 3; j++) arr[j*3]=j+3;console.log(arr);

Firefox:

Array [ <3 empty slots>, 4, <2 empty slots>, 5 ]

Node

[ , , , 4, , , 5 ]

IE Edge

[object Array][undefined, undefined, undefined, 4, undefined, undefined, 5]

Chrome

[3: 4, 6: 5]
Sign up to request clarification or add additional context in comments.

11 Comments

What is the reason for such a weird behaviour?
@tehect Weird? Clarity for me. Much easier than reading IE Edge's
It's not weird. Console is not part of the ecmascript standard, how sparse arrays are logged to the console is implementation dependant ... don't worry about it, all those arrays will behave identically
try var arr=[]; arr[1000000] = 1;console.log(arr); - the best output is chrome, firefox tries, bless it, IE gives up, and node ... still going :D
@raghavendra hurrah for Chrome's console.log superiority
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.