Glossary / Primitive values
Primitive values
JavaScript has seven primitive types: undefined, null, Boolean, Number, BigInt, String, and Symbol. Primitives are immutable values compared primarily by value, while objects are mutable reference values compared by identity. A variable does not have a permanent type; it refers to a value, and the same binding can refer to values of different types over time.
Type-by-type model
- Undefined commonly indicates an uninitialized binding, missing property, or omitted argument.
- Null conventionally represents intentional absence.
typeof nullreturning"object"is a historic compatibility quirk. - Boolean has two values,
trueandfalse, but conditions also coerce other values by truthiness. - Number is IEEE 754 binary64, including finite values,
NaN, and infinities. Integers are exactly safe only up to2^53 - 1in magnitude. - BigInt represents arbitrary-precision integers, but cannot implicitly mix with Number and is not JSON-serializable by default.
- String is an immutable sequence of UTF-16 code units; code units are not always user-perceived characters.
- Symbol is a unique primitive often used as a property key and for language protocols.
Values, bindings, and boxing
let value = 4;
value = "four"; // The binding changes which value it refers to.
const text = "hi";
text.toUpperCase(); // "HI"; the primitive itself was not mutated.
Property access on some primitives behaves as if a temporary wrapper were used, but wrapper objects such as new String("hi") are distinct mutable objects and should generally be avoided. const protects a binding from reassignment; it does not make an object or its properties immutable.
Comparisons and conversions
Primitive values generally compare by value, but Number has special cases: NaN !== NaN, and 0 === -0. Object.is and SameValueZero comparisons differ at these edges. Explicit conversion (Number, String, Boolean, BigInt) is clearer than relying on coercion, but conversion is not validation: Number("") is zero and parsing can accept formats you did not intend.
Practical guidance
Validate external values at boundaries with a schema or explicit checks. Model absence consistently (null versus omitted property). Choose Number, BigInt, or decimal-safe representation based on the domain. Avoid wrapper objects and avoid assumptions that strings or numeric values have properties they do not actually possess.
Interview questions
- Name JavaScript’s primitive types. Undefined, null, Boolean, Number, BigInt, String, and Symbol.
- Are primitives immutable? Yes. Operations produce new primitive values; property-like access does not mutate the primitive.
- Does
constmake an object immutable? No; it prevents rebinding the variable, while the object can still be changed. - Why can Number be unsafe for identifiers? Integers above
Number.MAX_SAFE_INTEGERmay lose exact identity; use BigInt or strings when needed. - Is
typeofa complete type checker? No. It has quirks such astypeof null === "object"and reports most objects generically.