What is the output of the following? “`js const handler = { get(target, key) { return key in target ? target[key] : 37; } }; const p = new Proxy({a: 1}, handler); console.log(p.a, p.b); “`

JavaScript Developer Hard

JavaScript Developer — Hard

What is the output of the following? “`js const handler = { get(target, key) { return key in target ? target[key] : 37; } }; const p = new Proxy({a: 1}, handler); console.log(p.a, p.b); “`

Key points

  • Proxy handler checks if property exists in target object
  • Returns property value if exists, otherwise returns 37
  • `p.a` exists in target object, so it returns 1
  • `p.b` does not exist in target object, so it returns 37

Ready to go further?

Related questions