JavaScript · Middle · Question 74 of 75
What does this code print?
const obj = {};
const a = { id: 1 }, b = { id: 2 };
obj[a] = "first";
obj[b] = "second";
console.log(obj[a]);- ✓Correct answer:
"second" - 2
"first" - 3
undefined
Explanation
Object keys are strings (or symbols); both objects become "[object Object]", so the second write overwrites the first. Use a Map for object keys.