Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Deep Clone

Data Structures, Operators & Stringsmedium

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 118: Enhanced Object Literals

Implement `deepClone(value)` returning a deep copy of a JSON-compatible value.

Sample tests

Input: deepClone({"a":{"b":1}})
Output: {"a":{"b":1}}
Input: deepClone([1,2,{"x":3}])
Output: [1,2,{"x":3}]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • This drops functions, undefined, Dates become strings, and it fails on cyclic data.
  • It is only safe for JSON-compatible values.

Learning resources

  • MDN: Object
Approach & explanation (try first)

Serializing then parsing produces a deep copy for plain JSON data, with the documented limitations.

Loading...
⌘/Ctrl + Enter

Run your code to see results.