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

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

  1. Name JavaScript’s primitive types. Undefined, null, Boolean, Number, BigInt, String, and Symbol.
  2. Are primitives immutable? Yes. Operations produce new primitive values; property-like access does not mutate the primitive.
  3. Does const make an object immutable? No; it prevents rebinding the variable, while the object can still be changed.
  4. Why can Number be unsafe for identifiers? Integers above Number.MAX_SAFE_INTEGER may lose exact identity; use BigInt or strings when needed.
  5. Is typeof a complete type checker? No. It has quirks such as typeof null === "object" and reports most objects generically.

References