Yes. I know I should never rely on for..in loop order, but when it comes to inconsistency of browsers it can be annoying.

I found it out when I wanted to fetch the first key of an object, but got different results on chrome (incorrect result) and other browsers.

Code:

1
2
3
4
5
6
7
8
9
10
11
console.log("wordsAsKeys");
var wordsAsKeys={ words:"value",as:"value",keys:"value" };
for (var k in wordsAsKeys) console.log(k);

console.log("numbersAsKeys");
var numbersAsKeys={ "3":"value","2":"value","1":"value" };
for (var k in numbersAsKeys) console.log(k);

console.log("mixedKeys");
var mixedKeys={ "3":"value",words:"value","1":"value" };
for (var k in mixedKeys) console.log(k);

The Result:

Firefox / Safari / IE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
wordsAsKeys
  words
  as
  keys

numbersAsKeys
  3
  2
  1

mixedKeys
  3
  words
  1

Chrome:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
wordsAsKeys
  words
  as
  keys

numbersAsKeys
  1
  2
  3

mixedKeys
  1
  3
  words

As you can see, the second and third loops enumerate on an object with numbers as indexes. What Google Chrome seem to do is treat the object as an Array, which outputs the numeric indexes first, in ascending order.

To me, it looks like a bug that may create inconsistency. Google & Chromium team - please fix it :)

Comments