A data type defines what kind of value a variable holds and which operations are allowed on it. In JavaScript there are eight data types: seven primitives (string, number, bigint, boolean, undefined, null, symbol) and one structured type, object. Types are attached to values, not variables, so one variable can hold a number now and a string later.
Knowing the types prevents bugs such as adding a string to a number by accident, and it explains why typeof null returns "object".
Primitives are immutable and are compared by value.
| Type | Example | Notes |
|---|---|---|
string | "hello" | Text, using single, double or backtick quotes |
number | 42, 3.14, NaN | 64-bit floating point, also used for integers |
bigint | 9007199254740993n | Integers of any size |
boolean | true, false | Logical values |
undefined | undefined | A declared variable with no value yet |
null | null | Intentional absence of a value |
symbol | Symbol("id") | Unique identifiers, often for object keys |
Because number uses floating point, 0.1 + 0.2 gives 0.30000000000000004, and integers are only exact up to Number.MAX_SAFE_INTEGER (2^53 - 1). Use bigint for larger integers and keep money in whole cents or use a decimal library.
Everything that is not a primitive is an object, compared by reference. Common forms include plain objects, arrays, functions, Date, Map, Set and RegExp.
typeof reports "object" for arrays, so use Array.isArray(scores) to test for an array.
typeof null === "object" is a long-standing quirk of the language that cannot be changed without breaking the web, so test for null with value === null.
JavaScript converts types automatically in some operations, which is useful but surprising.
Use === and !== by default, and convert explicitly with Number("5"), String(5) or Boolean(value).
TypeScript adds compile-time types on top of JavaScript and then removes them in the output:
The JavaScript that runs is still dynamically typed; TypeScript only catches mistakes before you run the code.
The meaning of "data type" is the same everywhere, but languages differ in how strict they are. C, C++ and Java declare types and check them at compile time, using sized types such as int, float and char. Python, like JavaScript, is dynamically typed, with types such as int, str, list and dict. Strongly and statically typed languages catch more errors early, while dynamic languages are quicker to write.
Eight: seven primitives (string, number, bigint, boolean, undefined, null, symbol) and object. Functions and arrays are objects.
undefined means a value has not been assigned, while null is a value you assign to mean "nothing". Both are falsy, and null == undefined is true but null === undefined is false.
Dynamically typed. Variables have no fixed type, and the engine checks types while the program runs.
NaN ("not a number") is a special floating-point value, so typeof NaN is "number". Test for it with Number.isNaN(value).
A data type describes the kind of value and the operations allowed on it. JavaScript has seven primitive types plus objects, uses dynamic typing with automatic coercion, and offers typeof for checks. Prefer strict equality, convert types explicitly, and consider TypeScript when you want types checked before runtime.