Quick answer
JavaScript has seven primitive types: string, number, bigint, boolean, undefined, symbol and null. Anything that is not one of these is an object, including arrays and functions.
The seven primitive types
| Type | What it holds | Example |
|---|---|---|
| string | Text. Strings cannot be changed in place | "hello" |
| number | Integers and decimals, stored as 64-bit floating point | 42, 3.14 |
| bigint | Whole numbers too large for number. Written with an n at the end |
9007199254740993n |
| boolean | true or false | true |
| undefined | A variable that has been declared but not given a value | let x; |
| symbol | A unique identifier, made with Symbol() |
Symbol("id") |
| null | An intentional empty value | null |
Numbers and precision
The number type uses 64-bit floating point, so some decimals cannot be stored exactly. For example, 0.1 + 0.2 gives 0.30000000000000004. Keep this in mind when you compare decimal values.
The bigint type exists for whole numbers that are too large for number to represent exactly. You cannot mix bigint and number values in the same arithmetic without converting one of them.
Checking types with typeof
The typeof operator reports the type of a value. Some results are surprising:
| Expression | Result |
|---|---|
typeof "hi" |
"string" |
typeof 42 |
"number" |
typeof true |
"boolean" |
typeof undefined |
"undefined" |
typeof null |
"object" |
typeof [] |
"object" |
typeof function () {} |
"function" |
typeof null returning “object” is a long-standing quirk of the language. To test for null, compare with the strict operator: value === null. Functions are objects too, even though typeof reports them as “function”.
Value versus reference
Primitives are compared by value. Objects, arrays and functions are compared by reference, which means two separate objects are never equal with ===, even if their contents match.
"a" === "a"; // true: same primitive value
[1] === [1]; // false: two different array objects
const a = {};
const b = a;
a === b; // true: both names point to the same object
Methods on primitives
Strings are primitives, yet you can call methods on them. For example, "hello".toUpperCase() returns "HELLO". JavaScript wraps the primitive in a temporary object for the call. The original string does not change, because the method returns a new string.
For the Java equivalent of this list, see what are the 8 primitive data types. For the wider comparison with non-primitive types, see primitive vs non primitive data types.