LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 8

Key order in JSON Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const o = { z: 1, 10: 1, a: 1, 2: 1 };
console.log(JSON.stringify(o));
  • A {"z":1,"10":1,"a":1,"2":1}
  • B {"2":1,"10":1,"z":1,"a":1} Correct
  • C {"10":1,"2":1,"z":1,"a":1}
  • D {"a":1,"z":1,"2":1,"10":1}
Correct Answer: Option B

Integer-like keys come first in ascending numeric order (2 before 10), then string keys in insertion order (z, a).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links