References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript structuredClone() method

Method JavaScript All modern browsers Updated
Quick answer

The structuredClone() function creates a deep copy of a value — nested objects and arrays are fully cloned, not shared. const copy = structuredClone(original). It handles Dates, Maps, Sets and more — far better than the old JSON.parse(JSON.stringify(x)) trick. It cannot clone functions.

Overview

structuredClone() makes a true deep copy of a value. Unlike a shallow copy (the spread operator, Object.assign()), which copies the top level but leaves nested objects shared by reference, structuredClone() recursively copies everything — so mutating the clone's nested data never affects the original. It's a built-in global function, no library needed.

It's the modern, correct replacement for the long-used hack JSON.parse(JSON.stringify(obj)). That trick deep-copies, but silently loses or corrupts a lot: undefined, functions, Date objects (they become strings), Map/Set, NaN/Infinity, and it throws on circular references. structuredClone() handles all of those correctly, including circular references.

It does have limits: it cannot clone functions or DOM nodes (it throws a DataCloneError), and it drops a class instance's prototype (you get a plain object with the same data). For plain data structures — the common case — it's exactly what you want. Reach for the spread operator when a shallow copy is enough, and structuredClone() when you need a genuine independent deep copy.

Syntax

const copy = structuredClone(value);

const original = { user: { name: "Ada" }, tags: [1, 2] };
const clone = structuredClone(original);
clone.user.name = "Grace";   // original.user.name stays "Ada"

Parameters

The structuredClone() method accepts the following parameters.

Parameter Description
value The value to deep-clone. Cannot contain functions or DOM nodes (those throw a DataCloneError).

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const original = { user: { name: 'Ada' }, when: new Date(2026, 0, 1) };

  const clone = structuredClone(original);
  clone.user.name = 'Grace'; // does NOT affect original

  document.getElementById('out').textContent =
    'original: ' + original.user.name + '\n' +
    'clone:    ' + clone.user.name + '\n' +
    'date kept: ' + (clone.when instanceof Date);
  // original: Ada / clone: Grace / date kept: true
</script>

Best practices

  • Use structuredClone() for a true deep copy of plain data, including nested objects.
  • Prefer it over JSON.parse(JSON.stringify(x)), which loses dates, Maps, undefined and more.
  • Use a shallow copy (spread {...obj}) when nested data doesn't need copying.
  • Don't pass functions or DOM nodes — they throw a DataCloneError.

Frequently asked questions

How do I deep-copy an object in JavaScript?
Use structuredClone(obj). It recursively copies nested objects and arrays so the copy is fully independent.
Why use structuredClone() instead of JSON.parse(JSON.stringify())?
The JSON trick loses Dates, Maps, undefined and functions, and throws on circular references. structuredClone() handles all of those correctly.
What can structuredClone() not copy?
Functions and DOM nodes (it throws a DataCloneError), and it drops class prototypes, giving a plain object.
What is the difference between a deep and shallow copy?
A shallow copy (spread, Object.assign()) shares nested objects by reference; a deep copy (structuredClone()) copies them too, so nothing is shared.