Weird associative array behavior in javascript

If I execute the code below on the chrome console, then I got the answer as an 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 is there such a difference? As far as I know, both use v8.

+4
source share
1 answer

Firefox says

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

IE Edge says

[object Array][undefined, 1, 2]

and, they are all correct

Chrome just DOES NOT report an empty index 0

Node indicates that index 0 is empty

Firefox tells you what's going on

Try the following:

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]

Chromium

[3: 4, 6: 5]
+5
source

All Articles