advanced
Deep Clone Object
Create a deep copy of a nested object.
Understand shallow vs deep copying.
📚 Concepts & Theory
Deep Clone
JSON.parse(JSON.stringify(obj))
// or structuredClone(obj) 🎯 Your Challenge
Write deepClone that creates a deep copy.
📝 Starter Code
JavaScript
function deepClone(obj) {
// Your code here
}
const original = {a: 1, b: {c: 2}};
const copy = deepClone(original);
Solution
JavaScript
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
const original = {a: 1, b: {c: 2}};
const copy = deepClone(original);
copy.b.c = 999;
console.log(original.b.c); // Still 2
Explanation
JSON stringify/parse creates deep copy.
❓ Frequently Asked Questions
Cannot clone functions or circular refs