- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Primitives and References
JavaScript Type System
JavaScript Primitives and References
Primitives are copied by value and objects are copied by reference.
This single difference explains a whole category of confusing bugs.
It is why changing one variable can change another.
Example
Example
javascript
let a = 1;
let b = a;
b = 2;
console.log(a);The output is 1: b got its own copy.
The Two Groups
Primitives: string, number, boolean, null, undefined, symbol, bigint.
Everything else is an object, including arrays and functions.
Assigning a primitive copies the value; assigning an object copies the reference.
