What are JS data types

Sunday 07/08/2022

·5 min read
Share:

JavaScript has eight data types: seven primitives and one object type. The primitives are string, number, bigint, boolean, undefined, null, and symbol. Everything else — arrays, functions, dates, maps, sets, regexes, your own class instances — is an object. The seven-primitive list is the modern (post-ES2020) answer; older articles often miss bigint, and many tutorials confusingly skip symbol. Below: each type, what typeof returns for it, and the gotcha that always shows up in interviews.

The seven primitives

Primitive values are immutable, compared by value, and stored directly (not by reference).

1. string

const s = 'hello'
typeof s // 'string'

UTF-16 code units. Single quotes, double quotes, and backticks all create strings — backticks add template literal support.

2. number

const n = 42
const f = 3.14
const inf = Infinity
const nope = NaN
typeof n    // 'number'
typeof nope // 'number' (yes, NaN is a number)

IEEE 754 double-precision floats. Range is roughly ±1.8e308, integer precision is exact up to 2⁵³ − 1 (Number.MAX_SAFE_INTEGER). NaN is a number — and NaN !== NaN, which is the most-asked JavaScript trivia question on earth.

3. bigint

const big = 9_007_199_254_740_993n
typeof big // 'bigint'

Added in ES2020. Arbitrary-precision integers, written with a trailing n. Use it when you genuinely need integer math beyond 2⁵³ − 1 — IDs from APIs that return 64-bit numbers, certain cryptographic operations, financial math at very large scales.

4. boolean

typeof true  // 'boolean'
typeof false // 'boolean'

Only two values. The interesting part is what coerces to which — the falsy list is false, 0, -0, 0n, "", null, undefined, NaN. Everything else is truthy, including the empty array [] and the empty object {}. That surprises people often.

5. undefined

let x
typeof x // 'undefined'

The value of any declared variable that hasn't been assigned, and the return value of any function without an explicit return. Distinct from nullundefined means "this slot was never set," null means "this slot was deliberately empty."

6. null

typeof null // 'object'  ⚠️

This is the canonical JavaScript bug. typeof null returns 'object' for legacy compatibility reasons — the first three bits of the value tag for null happened to match the tag for objects in the original 1995 implementation, and changing it would break the web. The cleanest null check is value === null.

7. symbol

const id = Symbol('user-id')
typeof id // 'symbol'

Unique, immutable identifiers. Useful as object keys when you want to attach metadata without name collisions, and as the basis for well-known protocols like Symbol.iterator. Most application code never creates a symbol directly; library code uses them more.

The one non-primitive: object

typeof {}            // 'object'
typeof []            // 'object'
typeof new Date()    // 'object'
typeof /regex/       // 'object'
typeof function(){}  // 'function' (special case)

Objects are mutable, compared by reference, and include arrays, dates, maps, sets, regexes, class instances, and plain objects. The typeof for function is 'function' — a special carve-out in the spec, even though functions are technically objects.

To distinguish object kinds, use Array.isArray(x), x instanceof Date, or Object.prototype.toString.call(x) (returns '[object Array]', '[object Date]', etc.).

Gotchas worth knowing

  • typeof null === 'object'. Use value === null for the strict null check. value == null (loose equality) is true for both null and undefined — that's the idiomatic "is this nullish" check.
  • typeof NaN === 'number'. Use Number.isNaN(x) (not the global isNaN, which coerces) to detect NaN reliably.
  • typeof [] === 'object'. Use Array.isArray(x) to distinguish arrays from other objects.
  • Boxed primitives are objects. typeof 'hi' === 'string' but typeof new String('hi') === 'object'. Don't use the wrapper constructors; they're a footgun left over from early JavaScript.
  • typeof of an undeclared variable returns 'undefined' without throwing. This is the one place where reading an undefined variable is safe.

Quick reference

| Value | typeof | Category | |---|---|---| | 'hi' | 'string' | primitive | | 42, NaN, Infinity | 'number' | primitive | | 42n | 'bigint' | primitive | | true, false | 'boolean' | primitive | | undefined | 'undefined' | primitive | | null | 'object' ⚠️ | primitive | | Symbol('x') | 'symbol' | primitive | | {}, [], new Date() | 'object' | object | | function(){} | 'function' | object (special) |

FAQ

How many data types are there in JavaScript?

Eight: seven primitives (string, number, bigint, boolean, undefined, null, symbol) and one object type. Older tutorials often miss bigint, which was added in ES2020.

What's the difference between null and undefined?

undefined means a variable has been declared but never assigned. null is an explicit "no value" set by the programmer. Both are falsy, and null == undefined is true under loose equality.

Why does typeof null return 'object'?

A bug from the original 1995 JavaScript implementation. The value tag for null happened to match the tag for objects. Fixing it would break a huge amount of existing code, so the spec preserves the behavior.

Is function a separate data type in JavaScript?

Technically no — functions are objects. But typeof function(){} returns 'function' as a special case in the spec, which makes type checks more convenient. Internally, functions are objects with a [[Call]] slot.

Share:
VA

Vadim Alakhverdov

Software developer writing about JavaScript, web development, and developer tools.

Related Posts