🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
JS MASTER CLASS /// MASTER THE ENGINE /// BUILD LOGIC /// ASYNC PATTERNS /// JS MASTER CLASS /// MASTER THE ENGINE ///

structuredClone() | JavaScript Tutorial - In-Depth Guide

Master structuredClone(): what types it supports, its handling of circular references, its limitations (no functions, no DOM nodes, no class prototypes), and when to still reach for a manual clone.

Total XP: 0|💻 javascript XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

System Hub

Core logic.

Quick Quiz //

Does structuredClone() preserve a cloned Date as an actual Date instance?


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

structuredClone() is a built-in, engine-level deep-copy function available in every modern JavaScript runtime. It correctly handles types the old JSON round-trip trick could not, and closes out this section's coverage of copying strategies.

1structuredClone() | JavaScript Tutorial - In-Depth Guide Part 1

structuredClone() is a global function that performs a true, native deep copy of most JavaScript values, no library required.

+
const original = { user: { name: 'Al', tags: ['a', 'b'] } };
const clone = structuredClone(original);
clone.user.name = 'Bo';
original.user.name; // still 'Al'
localhost:3000
🧬

Native Deep Clone

2structuredClone() | JavaScript Tutorial - In-Depth Guide Part 2

Unlike the JSON round-trip trick, structuredClone() correctly preserves Dates, Maps, Sets, RegExps, and typed arrays as their proper types.

+
const clone = structuredClone({ when: new Date(), tags: new Set(['a']) });
clone.when instanceof Date; // true
clone.tags instanceof Set;  // true
localhost:3000

Preserves Real Types

3structuredClone() | JavaScript Tutorial - In-Depth Guide Part 3

structuredClone() correctly handles circular references without throwing, unlike JSON.stringify which errors immediately.

+
const a = { name: 'a' };
a.self = a; // circular reference
const clone = structuredClone(a); // works fine
clone.self === clone; // true
localhost:3000

Handles Circular Refs

4structuredClone() | JavaScript Tutorial - In-Depth Guide Part 4

structuredClone() cannot clone functions, DOM nodes, or Error objects with full fidelity, and it throws a DataCloneError if you try.

+
structuredClone({ fn: () => {} }); // throws DataCloneError
localhost:3000

What It Can't Clone

5structuredClone() | JavaScript Tutorial - In-Depth Guide Part 5

structuredClone() also does not preserve custom class prototypes — a cloned class instance becomes a plain object with the same own properties.

+
class Point { distance() {} }
const p = new Point();
const clone = structuredClone(p);
clone instanceof Point; // false
localhost:3000

Loses Class Prototypes

6Step-by-Step Breakdown

structuredClone() is a global function that performs a true, native deep copy of most JavaScript values, no library required.

Unlike the JSON round-trip trick, structuredClone() correctly preserves Dates, Maps, Sets, RegExps, and typed arrays as their proper types.

Checkpoint: Does structuredClone() preserve a cloned Date as an actual Date instance?

  • Yes, unlike the JSON round-trip trick
  • No, it converts it to a string like JSON.stringify does

structuredClone() correctly handles circular references without throwing, unlike JSON.stringify which errors immediately.

structuredClone() cannot clone functions, DOM nodes, or Error objects with full fidelity, and it throws a DataCloneError if you try.

Checkpoint: What happens if you call structuredClone() on an object containing a function?

  • It throws a DataCloneError
  • It silently omits the function, like JSON.stringify

structuredClone() also does not preserve custom class prototypes — a cloned class instance becomes a plain object with the same own properties.

Next, we'll explore 'Array.prototype.reduce() Deep Dive'.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Use structuredClone() for Reliable Undo Snapshots in Accessible Editors

Because structuredClone() correctly preserves Dates and other structured data used in document metadata, it produces more reliable undo/redo snapshots for accessible editing tools than the lossy JSON round-trip trick would.

SEO Implications

  • 1

    No Direct SEO Effect

    structuredClone() is a runtime data utility; its SEO relevance is limited to preventing data-corruption bugs in server-side data processing.

Best Practices

Prefer structuredClone() Over the JSON Round-Trip Trick

It is native, correctly typed, handles circular references, and is available in all modern JS runtimes without needing a library — there is rarely a reason to still use JSON.parse(JSON.stringify(x)) for deep cloning.

Fall Back to Object.assign() or a Manual Clone When Class Identity Must Survive

If cloned data needs to remain an instance of its original class (with working methods), structuredClone() alone is not enough since it discards prototypes.

Frequent Bugs

THE BUG

Calling structuredClone() on a Redux/state object that happens to contain a function (like a memoized selector cached on the object) and getting an unexpected DataCloneError crash.

THE FIX

Ensure the data passed to structuredClone() contains only clonable types — strip out functions before cloning, or avoid storing functions inside state objects in the first place.

THE BUG

Cloning a class instance with structuredClone() and then calling one of its methods, only to get a TypeError because the clone is a plain object without the prototype.

THE FIX

Use Object.assign(new MyClass(), structuredClone(instance)) or a custom clone method on the class if the copy needs to remain a real instance.

Real-World Examples

Passing Complex Data to a Web Worker

An app needed to send a large, deeply nested dataset (including Dates and Maps) to a Web Worker for background processing.

worker.postMessage(structuredClone(largeDataset));
// postMessage uses the same structured clone algorithm internally

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

DataCloneError from cloning an object containing a function

const { onSave, ...clonable } = stateWithCallback; structuredClone(clonable);

The Solution //

Remove or replace any function properties before calling structuredClone(), since functions cannot be structurally cloned.

Lesson Glossary

[01]structuredClone()

A native global function that performs a deep copy using the structured clone algorithm.

Code Preview
structuredClone(x)

[02]Structured Clone Algorithm

The browser's internal algorithm for deep-copying values, also used by postMessage and IndexedDB.

Code Preview
postMessage()

[03]DataCloneError

The error thrown when structuredClone() encounters an unclonable value like a function.

Code Preview
DataCloneError

[04]Clonable Type

A value type supported by the structured clone algorithm: objects, arrays, Dates, Maps, Sets, RegExps, typed arrays, and more.

Code Preview
Map, Set, Date

[05]Unclonable Type

A value type the structured clone algorithm cannot represent: functions, DOM nodes, and some Error subclasses.

Code Preview
functions

Continue Learning