What is the output of the following? “`js const a = {x: 1}; const b = {x: 1}; const m = new Map(); m.set(a, ‘first’); m.set(b, ‘second’); console.log(m.size); console.log(m.get({x:1})); “`

JavaScript Professional Medium

JavaScript Professional — Medium

What is the output of the following? “`js const a = {x: 1}; const b = {x: 1}; const m = new Map(); m.set(a, ‘first’); m.set(b, ‘second’); console.log(m.size); console.log(m.get({x:1})); “`

Key points

  • Objects with the same properties are not considered equal in Maps
  • The size of the Map is 2 because both `a` and `b` are used as keys
  • Attempting to retrieve a value with a new object will not work

Ready to go further?

Related questions