Data Types
JavaScript has eight types: the seven primitives — string, number, boolean, null, undefined, symbol, bigint — plus object, which covers everything else including arrays and functions. Values carry types; variables do not.
Detailed Explanation
JavaScript is dynamically typed. A variable is a label, and the label has no type of its own, so let x = 1 followed by x = "one" is legal. The type belongs to the value sitting behind the label, and typeof asks the value, never the variable.
The seven primitives, with what each is actually for:
- string — text, stored as UTF-16 code units. Strings are immutable; every "modification" produces a new string.
- number — a 64-bit IEEE-754 double. There is no separate integer type, so
1and1.0are the same value. Integers are exact up toNumber.MAX_SAFE_INTEGER(2^53 − 1), and this type also carriesNaN,Infinity,-Infinity, and a distinct-0. - boolean —
trueandfalse. - undefined — "no value has been assigned yet". It is what you get from an unset variable, a missing parameter, a missing property, and a function with no
return. - null — "deliberately empty". The language never produces it by accident, so
nullin your data means a programmer put it there. - symbol — a guaranteed-unique value, used as a property key that cannot collide with anyone else's key. This is how
Symbol.iteratorhooks intofor...ofwithout occupying a normal name. - bigint — an arbitrary-precision integer, written with an
nsuffix (9007199254740993n). Use it for database IDs, currency in minor units, and anything beyond the safe integer range.
Everything that is not one of those seven is an object: plain objects, arrays, functions, Date, Map, Set, RegExp, Error, promises, and class instances. Objects are mutable and are handled by reference.
typeof is the usual first probe, and it has two rough edges. typeof null returns "object". That is a bug from the first implementation, where the type tag for objects was 0 and the null pointer was also 0. It cannot be fixed, because doing so would break code written over three decades. Test for null with value === null. The second rough edge is that typeof collapses all objects into "object" except functions, which report "function". To distinguish an array, use Array.isArray(value).
Precision is the other practical trap. Because numbers are binary floating point, 0.1 + 0.2 is 0.30000000000000004, so money should be stored as integer cents or as a bigint rather than as fractional dollars. And NaN is the one value not equal to itself, which is why Number.isNaN(value) exists.
The cost of dynamic typing is that type errors surface late, at the point where a wrong value is used rather than where it was produced. Runtime validation at your program's edges — parsed JSON, form input, HTTP responses — or a static layer such as TypeScript is how teams claw that back.
Code Examples
typeof across all eight types.
// typeof across all eight types.
console.log(typeof "hello"); // => string
console.log(typeof 42); // => number
console.log(typeof true); // => boolean
console.log(typeof undefined); // => undefined
console.log(typeof Symbol("id")); // => symbol
console.log(typeof 10n); // => bigint
console.log(typeof { a: 1 }); // => object
console.log(typeof null); // => object <- legacy bug, null is its own type
console.log(typeof [1, 2]); // => object <- arrays are objects
console.log(typeof function () {}); // => function <- the one object that reports differently
console.log(Array.isArray([1, 2])); // => true the reliable array checkNumbers are doubles. That has consequences.
// Numbers are doubles. That has consequences.
console.log(0.1 + 0.2); // => 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // => false
console.log(Number.MAX_SAFE_INTEGER); // => 9007199254740991
console.log(9007199254740993); // => 9007199254740992 <- silently rounded
// BigInt keeps large integers exact.
console.log(9007199254740993n); // => 9007199254740993n
console.log(2n ** 64n); // => 18446744073709551616n
// NaN is not equal to itself.
console.log(NaN === NaN); // => false
console.log(Number.isNaN(Number("abc"))); // => trueCommon Gotchas
`typeof null === "object"`
this is a preserved bug from 1995, not a statement about `null`'s type. Check for it directly with `value === null`, and remember that `typeof` therefore cannot tell you "this is a real object".
`typeof NaN` is `"number"`
`NaN` stands for "not a number" but is a numeric value, produced by operations like `Number("abc")` or `0/0`. Detect it with `Number.isNaN`, never with `=== NaN`.
BigInt and Number cannot be mixed in arithmetic
`1n + 1` throws `TypeError: Cannot mix BigInt and other types`. Convert explicitly with `BigInt(n)` or `Number(big)`, accepting that the second direction can lose precision.
`undefined` versus `null` by convention
let the language produce `undefined` for "not set", and use `null` yourself for "intentionally no value". Keeping that distinction makes `??` and optional chaining behave predictably.