Sub topic 2
Values Versus References
This chapter assumes you can declare variables, write functions, and create objects and arrays. It explains what happens when you assign or pass a value. That behavior depends on whether the value is a primitive or an object.
The Working Model
A variable that holds a primitive holds the value itself. When you assign it to another variable, JavaScript copies the value. A variable that holds an object holds a reference, which is a pointer to the object's location in memory. When you assign it, JavaScript copies the reference, and the object stays where it is. Both variables then point to the same object.
let a = 5;
let b = a;
b = 10;
console.log(a); // 5: primitives are copied by value
const obj1 = { count: 5 };
const obj2 = obj1;
obj2.count = 10;
console.log(obj1.count); // 10: both names refer to the same object
console.log({ x: 1 } === { x: 1 }); // false: different objectsPrimitives are also immutable. Methods such as "hi".toUpperCase() return a new string and leave the original unchanged. Objects are mutable, so any change made through one reference shows up through every other reference to the same object.
Comparison Uses Identity
Comparing two objects with === checks whether both sides are the same object, not whether their contents match. Two object literals with identical properties are two separate objects, so they compare as unequal. A later chapter covers equality in full. For now, the rule is that objects compare by identity.
Common Errors
Expecting `const` to freeze an object
const stops you from reassigning the variable. It does not stop you from changing the object the variable points to. In the example above, obj2.count = 10 is allowed even though obj2 is a const.
Mutating a function argument
JavaScript passes arguments by sharing. The function receives a copy of the reference, so changes to the object's properties are visible to the caller. Reassigning the parameter inside the function has no effect outside it.
function rename(user) {
user.name = "Ada"; // visible to the caller
user = { name: "Bob" }; // rebinds only the local parameter
}
const person = { name: "Grace" };
rename(person);
console.log(person.name); // "Ada"Trusting a shallow copy
Spread syntax and Object.assign copy only the top-level properties. Nested objects inside the copy are still shared with the original.
const original = { tags: ["a"] };
const shallow = { ...original };
shallow.tags.push("b");
console.log(original.tags); // ["a", "b"]
const deep = structuredClone(original);
deep.tags.push("c");
console.log(original.tags); // ["a", "b"]| Action | Primitive | Object |
|---|---|---|
| Assignment | Copies the value | Copies the reference |
| Passing to a function | Callee gets a copy | Callee shares the object |
| `===` compares | Values | Identity |
| Can be changed in place | No | Yes |
Exercise
What does this log? const list = [1, 2]; const alias = list; alias.push(3); const copy = [...list]; copy.push(4); console.log(list.length, copy.length, list === alias);
Show answer
It logs 3 4 true. alias points to the same array as list, so the push through alias changes list to length 3. The spread makes a new array of length 3, and pushing to it brings copy to 4 without touching list. list === alias is true because both variables refer to the same object.
Exercise
A function reset(config) runs config = {}. Why does the caller's object stay unchanged, and how would you clear its properties instead?
Show answer
The assignment points the local parameter at a new object and leaves the caller's reference alone. To clear the caller's object, change it in place, for example with for (const key of Object.keys(config)) delete config[key];.