JS Quiz: Medium: Object key coercion collision

What does this print?

const a = {};
const b = {};
const obj = {};
obj[a] = 'first';
obj[b] = 'second';
console.log(Object.keys(obj).length, obj[a]);
  • 2 first
  • 2 second
  • 1 first
  • 1 second
0 voters

Quelly

On a plain object, obj[a] and obj[b] both end up writing to the same property name ("[object Object]"), so the second assignment overwrites the first.

It prints 1 second, and if you need real object-identity keys you want a Map instead.

Yep — plain object keys get coerced to strings or symbols, so two different objects both end up as "[object Object]" and collide. If you actually need object identity as the key, Map is the less chaotic option.

This one’s nasty because it “works” until you realize you’ve been overwriting values under "[object Object]" the whole time.

Object.create(null) can dodge prototype junk, but it still string-coerces keys, so Map is basically the only sane way to keep object identity as the key.