Primitive copy
Strings, numbers, booleans and other primitives behave like independent values.
Primitives are copied as values. Objects are passed around by reference, which means two names can point to the same mutable object.
const a = { tasks: 12 };
const b = a;
b.tasks = 13;
console.log(a.tasks); // 13
Values & Types
When you assign a primitive value to another variable, the value is copied. Changing the new variable does not change the old one.
When you assign an object to another variable, the object itself is not copied. The new variable points to the same object. Mutating the object through either name changes what both names see.
This reference behavior is powerful and normal, but it is also the source of many bugs in state management, UI updates and data processing.
Strings, numbers, booleans and other primitives behave like independent values.
Objects, arrays, functions and dates are reference values.
Changing a property changes the object itself.
Use spread, structuredClone or deliberate mapping depending on depth and intent.
Examples
const originalUser = { name: "User A", tasks: 12 };
const updatedUser = { ...originalUser, tasks: 13 };
console.log(originalUser.tasks); // 12
console.log(updatedUser.tasks); // 13
const originalUser = { name: "User A", tasks: 12 };
const updatedUser = originalUser;
updatedUser.tasks = 13;
console.log(originalUser.tasks); // 13
Rules that matter
JavaScript lets values move flexibly, but professional code keeps meaning clear. Convert at boundaries, validate input and choose values that represent the real idea.
Primitive assignment copies the value. Object assignment copies the reference.
{} === {} is false because they are different object references.
Spread is useful, but nested objects still need deeper handling.
Functions that mutate arguments should be clearly named or avoided.
const can keep the same object reference while the object properties still change.
Independent updates are easier to reason about in interactive interfaces.
Production thinking
Reference behavior explains a huge percentage of JavaScript bugs. Once you see references, objects stop feeling mysterious.
UI state objects often control text, focus and visibility. Accidental mutation can show stale or incorrect feedback.
Production code should make mutation intentional. State bugs are expensive because they often appear only after several interactions.
If shared state corrupts rendered content, pages can show wrong titles, links or structured data.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Say the answer in plain language. If you can explain the value behavior, the code becomes much easier to debug.