Sub topic 1
Primitives, References, and Shared Objects
Every immutable data pattern in JavaScript depends on one rule: what does a variable actually hold? This chapter explains the difference between primitives and references. The later chapters on copying and immutable updates build on it.
Prerequisites
This chapter assumes you already know:
- JavaScript variables, functions, and scope
- How to create and read objects and arrays
- Arrow functions and ES module syntax
- What strict mode is, at a basic level
Two Kinds of Values
JavaScript values fall into two groups.
| Group | Types | What a variable holds |
|---|---|---|
| Primitives | `string`, `number`, `bigint`, `boolean`, `undefined`, `null`, `symbol` | The value itself |
| Objects | plain objects, arrays, functions, `Date`, `Map`, `Set`, and so on | A reference to the object |
A primitive is immutable by nature. You cannot change the number 1 or the string 'Ada'. You can only point a variable at a different value.
An object lives in memory on its own. A variable that "holds" an object actually holds a reference, which is the address of that object. Two variables can hold references to the same object.
Assignment Copies the Reference
When you assign one variable to another, JavaScript copies whatever the variable holds. For a primitive, that is the value. For an object, that is the reference, so you get no new object.
const a = { count: 1 };
const b = a;
b.count = 2;
console.log(a.count); // 2, because a and b reference the same object
let x = 1;
let y = x;
y = 2;
console.log(x); // 1, because primitives are copied by valueThe line b = a did not duplicate the object. It gave the object a second name. Writing b.count = 2 changed the one shared object, so the change shows up through a as well.
The primitive case works differently. y = 2 points y at a new value and leaves x alone.
Function Arguments Share References Too
Passing an argument to a function works like assignment. The parameter receives a copy of the reference, so the function sees the caller's object.
function rename(person) {
person.name = 'Grace';
}
const user = { name: 'Ada' };
rename(user);
console.log(user.name); // 'Grace', the function changed the caller's objectThis applies just as much to arrays, which are objects too. A function that calls push or sort on an array argument changes the caller's array.
Equality Compares References
The === operator compares primitives by value and objects by reference. Two objects with identical contents are still two different objects.
console.log('Ada' === 'Ada'); // true
console.log({ name: 'Ada' } === { name: 'Ada' }); // false, two separate objects
const p = { name: 'Ada' };
const q = p;
console.log(p === q); // true, same referenceCommon Errors
- Treating assignment as copying.
const copy = originalcreates an alias. Changes throughcopychangeoriginal. - Assuming a function argument is private. Changing a parameter's properties affects the caller.
- Comparing objects by contents with
===. Use===to check identity. Use a field-by-field comparison when you need to know whether two objects look the same. - Forgetting that arrays and nested objects are references. A value stored inside an object can also be shared with other code.
Exercise
After running const list = [1, 2]; const other = list; other.push(3); let n = list.length; n = 10;, what are list.length and other.length, and why?
Show answer
Both are 3. other references the same array as list, so push changes the one shared array. Assigning n = 10 only rebinds the primitive variable n and has no effect on the array.
Exercise
Why does [1, 2] === [1, 2] evaluate to false, while const a = [1, 2]; a === a evaluates to true?
Show answer
Each array literal creates a new object, so the first comparison checks two different references. In the second case both sides are the same reference.