FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Deep Clone

25mins

Implement a function deepClone(value) that creates a deep copy of a JavaScript value.

The function should:

  • Work with JSON-serializable values only (null, boolean, number, string, Array, Object).
  • Ensure the cloned value has no references to the original.
  • Changes to the cloned object should not affect the original.

Example

const obj1 = { user: { role: "admin" } }; const clonedObj1 = deepClone(obj1); clonedObj1.user.role = "guest"; console.log(obj1.user.role); // "admin" const obj2 = { foo: [{ bar: "baz" }] }; const clonedObj2 = deepClone(obj2); obj2.foo[0].bar = "bax"; console.log(clonedObj2.foo[0].bar); // "baz"

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests