Skip to content

OutputNot attempted

Object.assign is shallow

const obj1 = { a: 5, b: { c: 6 } };
const obj2 = Object.assign({}, obj1);
const obj3 = { obj1 };
obj1.b.c = 8;
console.log(obj2.b.c, obj3.obj1.b.c);
console.log(Object.assign({}, obj1, obj2, obj3));

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

Object.assign copies top-level properties only, so obj2.b is the SAME object as obj1.b and sees c = 8. Later sources win for duplicate keys; obj3 adds a property literally named obj1.