How to Deep Clone an Object in JavaScript?

Description:

Deep cloning creates an independent copy of an object, preventing reference issues.

Explanation:

Shallow copy methods like Object.assign() copy only the first level of properties, whereas deep cloning ensures nested objects are copied.

Example:

const obj = { a: 1, b: { c: 2 } };
const deepClone = JSON.parse(JSON.stringify(obj));
console.log(deepClone); // Output: { a: 1, b: { c: 2 } }

Other methods include structuredClone() and Lodash’s cloneDeep().