JavaScript Primitive Types
JavaScript has seven primitive data types: string, number, bigint, boolean, undefined, symbol, and null. Primitive values are not objects and are immutable.
A primitive is a basic JavaScript value that is not an object. The seven primitive types are `string`, `number`, `bigint`, `boolean`, `undefined`, `symbol`, and `null`.
`string` represents text, `number` represents both integer and floating-point numeric values, and `bigint` represents integers larger than the range safely represented by `number`. `boolean` has only `true` and `false` values.
`undefined` commonly indicates that a value has not been assigned, while `null` is an intentional absence of a value. `symbol` creates unique identifiers, often useful as object property keys.
Primitives are immutable: an operation that appears to modify a primitive instead produces another value. JavaScript can temporarily wrap primitives in corresponding object wrappers when properties or methods are accessed, but the primitive itself remains distinct from an object.
Key points
- JavaScript has seven primitive types: string, number, bigint, boolean, undefined, symbol, and null.
- Primitive values are immutable and are not objects.
- `number` uses the IEEE 754 double-precision floating-point representation.
- `bigint` is intended for integers beyond the safe integer range of `number`.
- `null` and `undefined` both represent absence-like states but have different meanings.
Interview questions
What are the primitive types in JavaScript?
JavaScript has seven primitive types: `string`, `number`, `bigint`, `boolean`, `undefined`, `symbol`, and `null`. They represent basic values rather than objects. Primitive values are immutable. One notable quirk is that `typeof null` returns `"object"` even though `null` is a primitive.
What is the difference between `null` and `undefined`?
Both can represent the absence of a useful value, but they are conventionally used differently. `undefined` commonly means that a value is missing or has not been assigned. `null` is generally assigned deliberately to indicate an intentional empty value. They are also distinct values, so strict equality with `===` returns false.
Why is `bigint` needed if JavaScript already has `number`?
JavaScript's `number` type uses double-precision floating-point arithmetic, so integers are represented exactly only up to `Number.MAX_SAFE_INTEGER` and down to its negative counterpart. `bigint` provides arbitrary-precision integer arithmetic for values outside that safe range. BigInts use an `n` suffix in literals, such as `9007199254740993n`. Arithmetic generally cannot directly mix `number` and `bigint` values.
Are primitive values immutable in JavaScript?
Yes, primitive values themselves are immutable. You cannot change the internal contents of a particular string or number value. For example, a string method such as `toUpperCase()` returns a new string rather than modifying the original string. Variables can still be reassigned to hold a different primitive value.
Why does `typeof null` return `"object"`?
It is a historical behavior of JavaScript that is retained for compatibility. The language specification still classifies `null` as a primitive value even though the `typeof` operator reports `"object"` for it. Therefore, `typeof` alone cannot reliably distinguish `null` from objects. A direct check such as `value === null` is appropriate when testing for `null`.