Data Types
10 min read
JavaScript's Eight Data Types
JavaScript is dynamically typed: variables do not have a fixed type, values do. There are seven primitive types and one object type.
number— integers and floats (64-bit IEEE 754)string— text, always immutableboolean—trueorfalseundefined— a variable that has been declared but not assignednull— an intentional absence of a valuebigint— arbitrary-precision integers, e.g.9007199254740993nsymbol— guaranteed-unique identifiersobject— everything else: plain objects, arrays, functions, dates, …
javascript
console.log(typeof 42); // "number"
console.log(typeof "hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (historical bug!)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"typeof null === "object" is a well-known quirk that dates back to JavaScript 1.0 and cannot be fixed without breaking the web. To check for null, use strict equality: value === null.
Primitives vs Objects
Primitives are immutable and compared by value. Objects are mutable and compared by reference — two objects with the same content are not equal unless they are the same object in memory.
javascript
const a = { x: 1 };
const b = { x: 1 };
console.log(a === b); // false — different references
const c = a;
console.log(a === c); // true — same referenceReady to test yourself?
Practice JavaScript— quiz & coding exercises