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 });

Explanation

The result is ["1", "2", "b", "a"]. If you need a dictionary with predictable insertion order for any keys, use Map.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 66

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz
Why does the order of keys in a JavaScript object not always match… - Algorithms and Data Structures Middle interview question