Question Bank
/

JavaScript Object Reference and Key Coercion Traces

JavaScript Object Reference and Key Coercion Traces

Six traces covering object-key stringification, reference vs literal equality, `Object.create` prototype chains, and shallow-copy aliasing with arrays.

Question Bank
Easy
JavaScript
6 questions
quiz
references
immutability
js-language

431 views

2

What does the following code print, and why does a[c] overwrite a[b]?

Examples

Example 1:

Input: a[b] = 123; a[c] = 456; log(a[b]); log(a[c]);
Output: 456; 456
Explanation: Plain-object keys are coerced to strings via toString, so both b and c become '[object Object]' and write to the same key.
let a = {};
let b = { key: 'b' };
let c = { key: 'c' };

a[b] = 123;
a[c] = 456;

console.log(a[b]);
console.log(a[c]);