EasyObjects📖 Theory Question

What is the difference between shallow copy and deep copy?

💡

Hint

Shallow copies references; deep copies recursively

Full Answer

Shallow copy: top-level properties only — nested objects are still shared references. Deep copy: recursively copies everything.

const orig = { a: 1, nested: { b: 2 } };

// Shallow — spread, Object.assign
const shallow = { ...orig };
shallow.nested.b = 99;
console.log(orig.nested.b); // 99 — orig mutated!

// Deep copy
const deep = structuredClone(orig); // ✅ modern recommended
// JSON.parse(JSON.stringify()) — simple but lossy (no undefined/Dates)

More Objects Questions

EasyHow does prototypal inheritance work in JavaScript?MediumWhat are property descriptors and property flags (writable, enumerable, configurable)?EasyWhat are getters and setters in JavaScript?EasyWhat is the difference between Object.freeze() and Object.seal()?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint