# References, Copying & structuredClone

> Stop accidentally mutating data! Learn how variables point to objects in memory, and how to safely copy them without causing side effects.

Subject: JavaScript | Lesson 19 of 63 | 8 min read
Interactive lesson: https://codewithanimation.in/javascript/object-references-and-copying
Previous: [Objects: Properties & Methods](https://codewithanimation.in/javascript/objects-fundamentals.md) | Next: [Destructuring, Spread & Rest](https://codewithanimation.in/javascript/destructuring-and-spread.md)

## Passed by Value vs Reference

In JavaScript, primitive types (numbers, strings, booleans) are passed by **value**. If you copy a string into a new variable, you get a completely separate duplicate.

Objects and Arrays are passed by **reference**. When you assign an object to a new variable, you aren't copying the data-you are just creating a new arrow that points to the exact same memory location.

## The Mutation Trap

Because variables just hold 'pointers' to the underlying object, modifying one variable will affect all others that point to the same object.

If `user1 = { name: 'Bob' }` and `user2 = user1`, then changing `user2.name = 'Alice'` will also change `user1`!

## Shallow vs Deep Copying

To avoid the mutation trap, you must explicitly copy the object.

**Shallow Copy** (using `{ ...obj }`): Creates a new top-level object, but if your object contains nested objects or arrays, those inner structures are still passed by reference.

**Deep Copy** (using `structuredClone(obj)`): Creates a 100% independent clone. Absolutely nothing is shared in memory.

## Common mistake: The old JSON hack

For a decade, the only way to deep copy an object natively was a terrible hack: `JSON.parse(JSON.stringify(obj))`. This destroyed Dates, Maps, Sets, and functions. Today, always use the built-in `structuredClone()` instead.

## Tip: Const doesn't protect objects

The `const` keyword only prevents you from reassigning the variable's *pointer*. It does NOT prevent you from mutating the object's *properties*. You can easily change `constUser.age = 50`.

## Try it: The Clone Visualizer

Compare Assignment (`=`), Shallow Copy (`{...}`), and Deep Copy (`structuredClone`) to see exactly how memory pointers are shared or detached.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/object-references-and-copying

## Check yourself

### Question 1

What happens when you do `const B = A` where A is an object?

- A. B becomes a new pointer to the exact same object in memory.
- B. B becomes a brand new, identical object.
- C. B becomes read-only.

Correct answer: A

Objects are passed by reference. `A` and `B` now both hold the 'remote control' to the exact same TV. Pressing buttons on either remote changes the same screen.

- Why not B: Incorrect. That only happens with primitive values (like numbers or strings).
- Why not C: Incorrect. The assignment operator does not freeze objects.

### Question 2

What is the danger of a Shallow Copy (`{...obj}`)?

- A. Nested objects and arrays are still shared by reference.
- B. It deletes functions from the object.
- C. It is much slower than a Deep Copy.

Correct answer: A

A shallow copy only duplicates the top-level keys. If any of those keys contain another object, the *reference* to that inner object is copied, meaning the inner object is still shared.

- Why not B: Incorrect. The spread operator copies functions perfectly fine.
- Why not C: Incorrect. Shallow copies are actually much faster because they do less work!

### Question 3

What is the modern, native way to Deep Copy an object in JavaScript?

- A. structuredClone()
- B. Object.assign()
- C. JSON.parse(JSON.stringify())

Correct answer: A

`structuredClone()` is a built-in global function available in modern browsers and Node.js that creates a perfect, independent deep copy of an object, preserving things like Dates and Maps.

- Why not B: Incorrect. `Object.assign()` only performs a shallow copy.
- Why not C: Incorrect. While this works for simple data, it destroys Dates, Sets, and Maps. `structuredClone` is the modern native solution.

## Remember this

- Primitives are passed by value; Objects and Arrays are passed by reference.
- Assigning an object to a new variable just creates a new pointer to the same data.
- A Shallow Copy (`{...obj}`) duplicates the top level, but shares nested objects.
- A Deep Copy (`structuredClone(obj)`) duplicates everything recursively, severing all ties.
