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
2
  • 1
    You can find more info about it here stackoverflow.com/questions/280713/… Commented Oct 1, 2011 at 11:41
  • @Narendra thanks, it's exactly what i wanted Commented Oct 1, 2011 at 11:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.