Course contentsShow
← JavaScript
Lesson 45 of 2,7232. Values and TypesFree lesson

Objects Store References, Not Values

Learn why assigning an object to a variable stores a reference to the object, not a copy of its data.

Objects Store References, Not Values

You'll learn: Why copying an object variable doesn't create a new, independent object—it creates another reference to the same one.

The Core Difference from Primitives

Remember how primitives (like numbers and strings) are copied by value? Objects work completely differently.

When you assign an object to a variable, JavaScript doesn't store the actual object data in that variable. Instead, it stores a reference—think of it as an arrow pointing to where the object lives in memory.

How References Work

let person = { name: "Alice" };
let friend = person;

friend.name = "Bob";

console.log(person.name); // "Bob" — wait, what?

This surprises beginners! When you wrote let friend = person, JavaScript didn't make a copy of the object. It copied the reference. Now both person and friend point to the same object in memory. Changing it through friend also changes it when accessed through person, because there's only one actual object.

The Post-It Note Analogy

Imagine the object is a house, and variables are post-it notes with the house's address written on them. When you do let friend = person, you write the same address on a new post-it note. Both notes point to the same house. Painting the house's door affects what you see from both post-it notes—because it's the same house.

Why This Matters

This behavior affects comparisons and modifications. Two variables pointing to the same object will reflect each other's changes, which can be powerful but also confusing if you expected independent copies.

Key Takeaway: Variables that hold objects store references (memory addresses), not copies of the object's data. Multiple variables can reference the same object.