Skip to content

JavaScript Data Types

The primitive and reference types in JavaScript, and how typeof helps you inspect them.

What you'll learn

  • The seven primitive types: string, number, boolean, null, undefined, symbol, bigint
  • The reference type: object (including arrays and functions)
  • How typeof behaves (and its famous quirk with null)
  • Type coercion: implicit vs explicit conversion
  • The difference between null and undefined

Concept

Primitives vs References

JavaScript values are divided into primitives and objects.

Primitives (7 types)

  1. string — text, like "hello" or 'world'
  2. number — integers and floats alike, like 42 or 3.14
  3. booleantrue or false
  4. null — an intentional "no value"
  5. undefined — a variable that has not been assigned a value
  6. symbol — a unique, immutable identifier (ES6)
  7. bigint — integers of arbitrary precision, like 9007199254740993n

Primitives are immutable — when you "change" a string, you are actually creating a new one.

Objects (the reference type)

Everything that is not a primitive is an object: plain objects, arrays, functions, dates, regexps, maps, sets, and more. Objects are mutable and stored by reference, so two variables can point at the same object.

const a = [1, 2, 3];
const b = a; // b points to the SAME array
b.push(4);
console.log(a); // [1, 2, 3, 4] — a changed too!

The typeof Operator

typeof returns a string describing the type. It works for all primitives except one famous quirk:

typeof "hi"     // "string"
typeof 42       // "number"
typeof true     // "boolean"
typeof undefined // "undefined"
typeof Symbol() // "symbol"
typeof 10n      // "bigint"
typeof {}       // "object"
typeof []       // "object"  ← arrays are objects
typeof null     // "object"  ← a historic bug, never fixed
typeof function(){} // "function"

null vs undefined

  • undefined means a variable has been declared but not assigned, or a function returned nothing.
  • null is an explicit "no value" that you assign yourself to signal emptiness.

Use === null or === undefined (strict equality) to check, not == null loosely.

Type Coercion

JavaScript automatically converts types in some operations, which can be surprising:

"5" + 3   // "53"  ← string concatenation
"5" - 3   // 2     ← numeric subtraction
0 == ""   // true  ← loose equality coerces
0 === ""  // false ← strict equality does not

Rule of thumb: always use === and !== (strict equality) to avoid coercion surprises. Convert types explicitly with Number(), String(), Boolean() when needed.

Example

              // Inspect types with typeof
const samples = [
  "hello",
  42,
  true,
  null,
  undefined,
  { name: "Ada" },
  [1, 2, 3],
  function () {},
  10n,
  Symbol("id"),
];

for (const value of samples) {
  console.log(typeof value, "→", value);
}

// null vs undefined
let notAssigned;
console.log(notAssigned);        // undefined
console.log(null);               // null
console.log(typeof null);        // "object" (historic bug)

// Strict vs loose equality
console.log(0 === "");   // false
console.log(0 == "");    // true  ← avoid ==
            

typeof is the quickest way to inspect a value's type. Remember the null quirk and always prefer === over == to avoid implicit type coercion.

Try it

  • JSON Formatter

    JavaScript objects and arrays are JSON-like — format and inspect them.

  • JSON Validator

    Validate the structure of JSON data you build in JavaScript.

Common mistakes

The mistake

Using == instead of === and getting surprised by type coercion

The fix

Always use === and !== (strict equality). They do not coerce types, so 0 === '' is false. Convert types explicitly when you need to.

The mistake

Expecting typeof null to be 'null'

The fix

typeof null returns 'object' due to a historic bug in the original JavaScript engine. Check for null explicitly with value === null.

Related Tools

Related Cheatsheets

Practice JavaScript Data Types

1 exercise

Practice JavaScript Data Types

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors