JavaScript Data Types
The primitive and object types JavaScript works with, and how typeof reports each one.
2 min read
JavaScript values fall into two categories: primitives, which are immutable and compared by value, and objects, which are mutable and compared by reference. Everything you work with is one or the other.
The primitive types
typeof "hello" // "string"
typeof 42 // "number"
typeof 3.14 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof Symbol() // "symbol"
typeof 10n // "bigint"That's six of the seven primitives — the seventh, null, is a special case covered below. Two are worth calling out:
number— JavaScript has only one numeric type. There's no separateintorfloat;42and3.14are both justnumber, stored as 64-bit floating point. This is why0.1 + 0.2famously gives0.30000000000000004instead of0.3— the same floating-point rounding every language with this number format has.bigint— added for numbers too large to represent safely as anumber, written with annsuffix (10n).
The null quirk
typeof null // "object"This is a long-standing bug in the language, present since the first version and never fixed because doing so would break too much existing code. null is still a primitive, not an object — typeof is just wrong about it. null represents an intentionally empty value; undefined represents a value that was never set.
let assigned; // undefined -- declared, no value given
let empty = null; // null -- deliberately set to "nothing"The object type
Everything else — plain objects, arrays, functions, dates — is a typeof "object" (functions are a special case: typeof reports them as "function" even though they're technically objects too).
typeof {} // "object"
typeof [] // "object" -- arrays are objects
typeof function () {} // "function"Unlike primitives, objects are compared by reference, not by value:
console.log({ a: 1 } === { a: 1 }); // false -- two different objects in memory
console.log(1 === 1); // true -- primitives compare by valueTwo object literals with identical contents are still two separate objects — this trips up a lot of beginners writing equality checks against arrays or objects.
Knowing a value's actual type matters most at the boundary between types — the next lesson covers what happens when JavaScript is asked to compare or combine values of different types.