🟑 MediumClosures & ScopeπŸ’» Output Question

Map key reference equality

πŸ’‘

Hint

Map keys use Object.is (reference equality) for objects. Two objects that look equal are different keys unless they're the same reference.

What does this output?

const map = new Map();

const key1 = { id: 1 };
const key2 = { id: 1 };

map.set(key1, 'value1');
map.set(key2, 'value2');

console.log(map.size);
console.log(map.get(key1));
console.log(map.get({ id: 1 }));

Correct Output

2
value1
undefined

Why this output?

Explanation: key1 and key2 look the same but are different object references. Map uses reference equality (Object.is) for object keys. Two separate entries. A new { id: 1 } object has no match β€” returns undefined.

Key Insight: Map keys use Object.is (reference equality) for objects. Two objects that look equal are different keys unless they're the same reference.

More Closures & Scope Output Questions

🟒 EasyClassic var in loop closureβ†’πŸŸ’ Easylet in loop closure (fix)β†’πŸŸ‘ MediumClosure counterβ†’πŸŸ‘ MediumIIFE closureβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz