Typorithm · easy

Deep Clone

structured-cloneinterview-questionsjson-clonedeep-copy
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Deep Clone

0%

Copy & Equality

structuredClone(obj) is the modern way to deep-clone: it recurses through nested objects and even handles cycles. The older JSON.parse(JSON.stringify(obj)) trick also deep-copies but drops functions and undefined and turns Dates into strings.

structuredClone(obj) produces a fully independent deep copy — every nested object and array is cloned, and it can handle cyclic references that would crash JSON.

The classic JSON.parse(JSON.stringify(obj)) trick deep-copies anything JSON can represent, but it has real limits: functions and undefined values are dropped, and Date objects come back as strings.

Prefer structuredClone for correctness. Reach for the JSON trick only for plain, JSON-safe data, and never when the object contains functions, Dates, Maps, or cycles.

01

structuredClone deep-copies nested objects and handles cycles.

02

JSON.parse(JSON.stringify(obj)) deep-copies only JSON-safe data.

03

The JSON trick drops functions/undefined and stringifies Dates.

const obj = { when: new Date(), tags: ["a", "b"] }

const deep = structuredClone(obj)

// older trick — has limits
const viaJson = JSON.parse(JSON.stringify(obj))
// functions dropped, undefined dropped, Date becomes a string

Read the implementation

Connect each part of the code to the concept before you type.

1. structuredClone

structuredClone recursively clones nested objects and even handles cyclic references.

const deep = structuredClone(obj)

2. JSON round-trip

Serializing to JSON and back deep-copies, but only data that survives JSON.

const viaJson = JSON.parse(JSON.stringify(obj))

3. JSON limits

The JSON trick drops functions and undefined, and turns Dates into strings.

// functions dropped, undefined dropped, Date becomes a string

4. A Date value

structuredClone preserves the Date object; JSON would flatten it to a string.

new Date()

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Deep Clone: Lesson & Practice · Typorithm | ApplyDjinn