What does the following code output? const handler = { get: (t, k) => k in t ? t[k] : 37 }; const p = new Proxy({}, handler); p.a = 1; console.log(p.a, p.b, ‘c’ in p);

JavaScript Professional Hard

JavaScript Professional — Hard

What does the following code output? const handler = { get: (t, k) => k in t ? t[k] : 37 }; const p = new Proxy({}, handler); p.a = 1; console.log(p.a, p.b, ‘c’ in p);

Key points

  • Proxy object with custom handler returns value if property exists, else returns 37
  • p.a outputs 1 because property 'a' is set to 1
  • p.b outputs 37 because property 'b' doesn't exist
  • 'c' in p checks if property 'c' exists in p, which is false

Ready to go further?

Related questions