Algorithms and Data Structures · Middle · Question 66 of 75
Why does the order of keys in a JavaScript object not always match insertion order?
Object.keys({ b: 1, 2: 1, a: 1, 1: 1 });- 1Keys are always sorted alphabetically by the engine
- 2The order is random and may change between runs
- ✓Correct answer: Integer-like keys go first in ascending order, then the rest
Explanation
The result is ["1", "2", "b", "a"]. If you need a dictionary with predictable insertion order for any keys, use Map.