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

JS Quiz answer: Option 4 (D).

Correct choice: 1 second

Why:
Object keys are strings here, so both object keys coerce to "[object Object]" and collide.

Go deeper:

Strings In JavaScript Temp

First-answer leaderboard

  1. @kirupa - 3 (firsts) :trophy:
  2. @emmawalter5 - 1 (first)