JavaScript passes objects by reference, not by value. Every time you write const copy = { ...original }, you create a shallow copy — a new object whose top-level properties point to the same underlying values as the original. For primitive properties like numbers and strings that is fine. But for nested objects and arrays, both copy and original share the exact same reference. Mutate one and you silently mutate the other.
The shallow copy pitfall
The spread operator, Object.assign, Array.slice(), and [...arr] all perform shallow copies. They are fast and sufficient for flat data, but dangerous as soon as nesting appears. This causes real bugs: mutating nested state in React without a proper deep clone triggers stale renders; config objects shared across modules accumulate unexpected mutations that are nearly impossible to trace.
const config = { server: { port: 3000, host: "localhost" } };
// Shallow copy — both config and dev share .server reference
const dev = { ...config };
dev.server.port = 4000;
console.log(config.server.port); // 4000 — MUTATED! Bug!
// Deep copy — fully independent
const prod = structuredClone(config);
prod.server.port = 8080;
console.log(config.server.port); // 4000 — unchanged ✓Why this topic dominates senior interviews
Deep clone, deep equal, and path-by-string access appear in over 80% of senior frontend engineering interviews. They test whether you understand JavaScript's memory model, whether you can reason about recursive algorithms, and whether you can handle edge cases like circular references, Date objects, RegExp instances, and null — all the cases that naive implementations break on.