LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 2

The keys 1 and "1" Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

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

main.js
const o = {};
o[1] = "a";
o["1"] = "b";
console.log(Object.keys(o).length, o[1]);
  • A 2 a
  • B 2 b
  • C 1 a
  • D 1 b Correct
Correct Answer: Option D

The number 1 is converted to the string "1", so both assignments target the same property.

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