Every value in JavaScript has a type. There are two groups: primitive values (simple, single values) and objects (everything else, such as arrays and functions).
The seven primitives
| Type | Example | Used for |
|---|---|---|
string | "hello" | Text |
number | 42, 3.14 | Most numbers |
bigint | 9007199254740993n | Very large whole numbers |
boolean | true, false | Yes or no |
undefined | undefined | “No value has been set” |
null | null | “Intentionally empty” |
symbol | Symbol("id") | Unique identifiers |
const name = "Maya"; // string
const age = 28; // number
const isStudent = false; // boolean
let nothingYet; // undefined
const empty = null; // null
console.log(name, age, isStudent, nothingYet, empty);
Primitives are immutable
You cannot change a primitive value. You can only replace it with another.
let word = "cat";
word[0] = "b"; // silently ignored
console.log(word); // → cat
word = "bat"; // replacing the whole value works
console.log(word); // → bat
String methods never change the original. They return a new string.
Primitives vs objects
Primitives are compared and copied by value. Objects are handled by reference, which means two variables can point at the same object.
let a = 5;
let b = a;
b = 10;
console.log(a, b); // → 5 10
const first = { n: 1 };
const second = first; // both names point at the same object
second.n = 99;
console.log(first.n); // → 99
Changing second changed first, because they are the same object. You will see this again in the Objects chapter.
The next lessons look at each type in turn: undefined and null, booleans, numbers, strings, BigInt and Symbol.