Reference vs Value & The Const Mutation Trap

Why arr.push works on a const array, why two equal-looking objects aren't ===, and the immutable update pattern React relies on.

Step 1 of 6

Two types of variables: primitives and objects

JavaScript has seven primitive types: number, string, boolean, null, undefined, symbol, bigint. When you assign one to a variable, you store the value itself.

Everything else (objects, arrays, functions, Maps, Sets, classes) is an object. When you assign one, you store a reference to it. Two variables can point at the same object; mutating through one is visible through the other.

Think of it this way: A primitive is like writing a number on a sticky note — copying the note copies the number. An object is like writing the address of a house on a sticky note — copying the note still points at the same house.
Learn more on MDN