LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 16

A property added with defineProperty Answer, explanation & discussion

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

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

main.js
const o = {};
Object.defineProperty(o, "id", { value: 7 });
o.id = 9;
console.log(o.id, Object.keys(o), JSON.stringify(o));
  • A 9 [ 'id' ] {"id":9}
  • B 7 [ 'id' ] {"id":7}
  • C 9 [] {}
  • D 7 [] {} Correct
Correct Answer: Option D

Without explicit flags the property is non-writable (the assignment is silently ignored) and non-enumerable (hidden from keys and JSON).

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