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.