Deep Clone Object
JavaScript
medium
25 mins
Create a function that performs a deep clone of a given JavaScript object. The function should handle nested objects, arrays, and primitive data types without retaining any references to the original object.
Example Inputs & Outputs
const original = { a: 1, b: { c: 2 }, d: [3, 4] }; const cloned = deepClone(original); console.log(cloned); // { a: 1, b: { c: 2 }, d: [3, 4] } console.log(cloned !== original); // true console.log(cloned.b !== original.b); // true console.log(cloned.d !== original.d); // true
Constraints & Edge Cases
- The function should handle objects and arrays of arbitrary depth.
- It should not modify the original object.
- The function should correctly clone primitive values (number, string, boolean, null, undefined, symbol).
- Functions and special objects (like Date, RegExp) are not required to be cloned accurately.
Companies:
google
airbnb
meesho
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
