What is the output of the following? “`js const obj = {}; Object.defineProperty(obj, ‘x’, { get() { return 42; }, set(v) { console.log(‘set:’, v); } }); obj.x = 10; console.log(obj.x); “`

JavaScript Developer Hard

JavaScript Developer — Hard

What is the output of the following? “`js const obj = {}; Object.defineProperty(obj, ‘x’, { get() { return 42; }, set(v) { console.log(‘set:’, v); } }); obj.x = 10; console.log(obj.x); “`

Key points

  • The setter function logs the value being set
  • The getter function returns 42
  • The order of operations is set, then get
  • The output includes both the set value and the final value

Ready to go further?

Related questions