1

So I have some code like this:

var obj = {
    something: 10,
    1 : 15,
    2 : 20,
    3 : 25
};
for(var i in obj) {
    console.log(i);
}

That returns: 1, 2, 3, something

Why is the first element moved to the end?

Ollie
  • 47
  • 4
  • 3
    the specs says so ... index like values comes first. – Nina Scholz Oct 25 '20 at 16:44
  • 1
    It's best if you don't rely on object property order. It *could* be predictable but it's also very easily disturbed. Use at least a map which guarantees insertion order. It might be even better if you use a different data structure like an array to maintain the order you expect. – VLAZ Oct 25 '20 at 16:47

0 Answers0