Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 14 / 31DATA AND OBJECTS

The cost of a copy.

Spread copies the outer container, not an entire object graph.

01 / THE BASICS

Start here.

Mutation means changing an existing object or array. If user.name = 'Grace' runs, every name that refers to that user object now sees Grace. A copy creates a new container so that some changes can be independent. For a flat object, const next = { ...user }; makes a new outer object containing the same property values. next !== user is true because they are separate objects.

The spread syntax ... copies only one layer. If a property itself contains an object, the copy receives a reference to that same inner object. In const old = { user: { name: 'Ada' } }; and const next = { ...old };, old.user and next.user still refer to one nested object. Changing next.user.name therefore also changes old.user.name. This is a shallow copy.

If the previous state must stay unchanged, copy each layer along the path you will edit. A new outer object and a new user object let the name change without changing old. Copying is not a rule to apply blindly: direct mutation is fine when one part of a program owns the object and no one needs an earlier snapshot. The important skill is to know whether two names share the same object before you change it.

02 / THE IDEA

Build the model.

Mutation changes an existing object. Copying makes a new outer object so later changes can be isolated. Spread syntax and array slicing make shallow copies: nested objects remain shared unless you copy them as well.

Neither mutation nor copying is automatically correct. Mutation can be direct and efficient when ownership is clear. Copies help when a previous state must remain meaningful, as in interface updates. Before changing nested data, trace which references still point to the same inner object.

03 / UNDER THE SURFACE

How it works.

Copying a container creates a new outer identity. Object spread copies the original object's own enumerable properties into a new object; array spread places the source elements into a new array. If a copied property is itself an object, both containers still refer to that nested object. This is why a shallow copy protects top-level changes but not every deeper change.

When updating nested state, copy every layer along the path you change. That sounds elaborate, but it lets a reader compare old and new values without old suddenly shifting underneath them. For a simple local object with one owner, direct mutation can be clearer. The important question is whether any other part of the program expects the old state to remain stable.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const old = { user: { name: 'Ada' } };
const next = { ...old };
next.user.name = 'Grace';

First notice. old.user.name also changes because user was not copied.

next = {...old} creates a new outer object. Its user property is assigned the same nested object referenced by old.user. next.user.name = 'Grace' changes that nested object, so old.user.name also becomes 'Grace'. old !== next is true, but old.user === next.user is also true.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const old = { user: { name: 'Ada' } };
const next = { ...old, user: { ...old.user, name: 'Grace' } };
console.log(old.user.name, next.user.name);

The output is 'Ada' and 'Grace'. The first spread creates a new outer object, and the second creates a new user object. The changed name is put on the new nested object, leaving old.user untouched. This is still only a copy of the layers shown.

06 / KEEP THIS

One thing to remember.

A shallow copy creates one new layer of identity.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.