0

I already know how to enumerate an object in javascript. My question is what's the key sequence while enumerating.Is it by A-Z or time ordered?

code

var a = { 
          "a":""
         ,"b":""
         ,"c":""};
for (var k in a) {
    console.log(k);
} 

output

a,b,c

code

var a = { 
          "b":""
         ,"a":""
         ,"c":""};
for (var k in a) {
    console.log(k);
} 

output

b,a,c

code

var a = { 
          "b":""
         ,"a":""
         ,"c":""};
a.d = "";
for (var k in a) {
    console.log(k);
} 

output

b,a,c,d
Community
  • 1
  • 1
wukong
  • 2,430
  • 2
  • 26
  • 33

1 Answers1

4

Usually the order is the time it was added, but the specification for the for in loop says:

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified.

So you cannot really rely on one specific order.

pimvdb
  • 151,816
  • 78
  • 307
  • 352