Skip to content

OutputNot attempted

Spread vs structuredClone

const original = { a: 1, nested: { b: 2 }, list: [1] };
const spread = { ...original };
const cloned = structuredClone(original);
spread.a = 100;
spread.nested.b = 200;
cloned.list.push(2);
console.log(original.a, original.nested.b, original.list.length);

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.

Spread is a SHALLOW copy: top-level a is copied, but nested is the same object, so 200 leaks into original. structuredClone is deep, so pushing to its list does not affect the original.