Question presented to candidate: "If you use an object as a key on a plain JavaScript object, like obj[someObject] = value, what actually happens to that key? Would a Map handle it differently?"
What a strong answer should cover:
- 📌 Interview term: object key coercion — verified directly: using an object as a key on a plain
{}genuinely coerces it to the literal string"[object Object]"— every distinct object used as a key this way collides on the exact same string, a real, silent data-corruption bug. - 📌 Interview term:
Map's real, direct answer to the prompt — verified directly: aMapgenuinely accepts the object itself, with no coercion at all, as a real, distinct key — two different object references are genuinely two different, non-colliding keys. - 📌 Interview term:
Maphas a real, direct.size— verified directly: unlike a plain object (which requiresObject.keys(obj).lengthto count entries), aMapgenuinely has a real.sizeproperty, always accurate. - 📌 Interview term:
Setdedupes while keeping type-DISTINCT values separate — verified directly: aSetbuilt from[1, "1", 1, 2, 2]genuinely keeps both the number1and the string"1"as two SEPARATE entries (using SameValueZero comparison, the same algorithm covered in this bank's ownincludesvs.indexOfquestion), while still correctly removing the genuine duplicate1s and2s. - A precise answer names that
Map/Setalso genuinely guarantee real insertion-order iteration (a formal spec guarantee, not just an implementation detail) and are directly iterable viafor...of— while a plain object/array can be used similarly in PRACTICE for simple cases, but without the same formal guarantees or capabilities (arbitrary key types, guaranteed size, no prototype-pollution risk from inherited properties).
Clarifying questions expected:
- None — this is a definitional/comparison question; directly answering the prompt's object-key coercion scenario (and Map's real fix) is the strong signal.
Code / implementation expected: Yes — the side-by-side object-key-coercion vs. Map-object-key test is the clearest, most convincing demonstration.