Problem
Implement deepClone(value) that returns a completely independent copy of value.
Shallow copies ({...obj}, Object.assign, [...arr]) only copy the top level — nested objects and arrays are still shared references, so mutating the copy corrupts the original. You need a recursive clone for safely snapshotting state (undo/redo, immutable updates, caching API responses).
deepClone(value) must:
- Return primitives (
string,number,boolean,null,undefined,symbol,bigint) unchanged. - Recursively clone arrays and plain objects.
- Never mutate the source, and never share nested references with it.
Input
const original = { a: 1, b: { c: 2, d: [3, { e: 4 }] } };
const copy = deepClone(original);
copy.b.d[1].e = 99;
Expected output
original.b.d[1].eis still4(source untouched)copy.b.d[1].eis99
Implement from scratch (no structuredClone / JSON.parse(JSON.stringify(...))):
function deepClone(value) {
// Your code here
}