🟢 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