Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

In what order does Object.keys return keys?

Do you know what will be the output of this code?

JavaScript
const obj = { b: 1, 2: "x", a: 2, 1: "y" };
console.log(Object.keys(obj));
Show answer and explanation

The answer is ["1", "2", "b", "a"].

Integer-like keys are listed first in numeric order. Other string keys follow in the order they were added.

Take the full quiz Next question →

More Objects questions

  1. Why do object keys 1 and "1" overwrite each other?
  2. What happens when you use an object as an object key?
  3. Why is {} === {} false in JavaScript?
  4. Are JavaScript objects copied by reference?
  5. Does the spread operator make a deep copy?

Related topics