LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 3

Objects as keys Answer, explanation & discussion

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

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

main.js
const store = {};
const a = {};
const b = { x: 1 };
store[a] = "first";
store[b] = "second";
console.log(store[a], Object.keys(store));
  • A second [ '[object Object]' ] Correct
  • B first [ 'a', 'b' ]
  • C first [ '[object Object]', '[object Object]' ]
  • D second [ 'b' ]
Correct Answer: Option A

Both objects are converted to the same string key, "[object Object]", so the second assignment overwrites the first. Use a Map for object keys.

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