StoicBuilder

HandbookI the LudusValue vs reference

Ithe Ludus, Read code

Value vs reference

1 min readUpdated 28 November

In plain words

Numbers and text are copied when you assign them, but objects and arrays are shared, so two names can point at the very same thing.

Where you see it

Maya edits a draft of a Quill note, clicks Cancel, and the saved note has changed anyway. The draft was never a copy: both names pointed at one object, so every edit landed on both.

src/components/NoteEditor.tsxbrowser
const draft = note; // same object, two namesdraft.title = "New title";note.title; // "New title" as well const copy = { ...note }; // a new objectcopy.title = "Other";note.title; // still "New title"
= gives an object a second name. Spread makes a new one.

What goes wrong

  1. Editing a "copy" that's really the original

    const draft = note copies nothing. Cancel buttons and undo break, because the original was being edited the whole time.

  2. Changing React state in place

    posts.push(newPost) changes the existing array, so React sees the same array and may not re-render. Make a new one: setPosts([...posts, newPost]).

    posts.push(newPost); setPosts(posts);
  3. Comparing objects with ===

    === checks whether two names point at the same object, not whether the contents match. Two orders with identical fields are still not ===.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Never change state or props in place in this component; build a new object or array for every update. Point out any line where two variables share one object by accident.

Quick check

Sign in to earn +10 XP for it

In Quill, Maya edits a draft and clicks Cancel, but the saved note keeps her edits. The code starts with const draft = note. What's wrong?

Got a prompt that touches value vs reference?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.