Skip to the solution
MediumFrontend6 min read

How do you deep clone an object in JavaScript?

586 views

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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 79 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Why is navigator.sendBeacon() the right tool for sending analytics data when a user closes a tab, instead of a normal fetch call?

Continue