Skip to solution
mediumFrontend

How do you deep clone an object in JavaScript?

586 views
01

Understand the problem

Question presented to candidate: "If your object has a circular reference — an object that references itself somewhere inside its own nested structure — would JSON.parse(JSON.stringify(obj)) work to deep clone it? What would you actually use instead?"

What a strong answer should cover:

  • 📌 Interview term: shallow copy vs. deep clone — a shallow copy ({ ...obj }, Object.assign) only copies the TOP-level properties; any NESTED object/array is still the SAME shared reference — verified directly: mutating a nested value through a shallow copy genuinely leaked back and changed the original.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: JSON.stringify() genuinely throws a real TypeError on a circular reference — it cannot serialize an object that references itself, so JSON.parse(JSON.stringify(obj)) is not just imperfect for circular structures, it genuinely fails outright.
  • 📌 Interview term: structuredClone() — the modern, native, built-in deep-clone function — verified directly: it genuinely produces fully independent, deeply-copied nested references (a mutation on the clone genuinely does NOT leak back to the original), and it genuinely handles a circular reference correctly, with no error.
  • 📌 Interview term: what structuredClone can and can't clone — verified directly: it correctly preserves real Date objects as actual Date instances (unlike JSON.stringify, which turns a Date into a plain string, verified directly as a second, real advantage over the JSON trick) — but it genuinely throws on a function, since code itself cannot be cloned.
  • A precise answer names that JSON.parse(JSON.stringify()) remains a real, valid quick option specifically when the data is known in advance to be plain, JSON-safe data (no functions, no Dates, no circular references, no undefined values) — otherwise structuredClone() is the correct, modern, general-purpose tool.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering the prompt's circular-reference scenario (and naming the correct modern fix) is the strong signal.

Code / implementation expected: Yes — the real circular-reference test (JSON throwing vs. structuredClone succeeding) is the clearest, most convincing demonstration.

copy
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every leak, throw, and preservation claim below was actually run in Node.

1. Why This

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: JSON.stringify genuinely throws on a circular reference while structuredClone genuinely handles it, plus a real shallow-copy mutation leak
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 79 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track