Data Types

Every value in JavaScript has a "type" — a category that decides what you're allowed to do with it.

What Are Data Types?

A data type tells JavaScript (and you) what kind of value you're working with — a number, some text, a yes/no, and so on. JavaScript sorts values into two big families:

  • Primitive types — simple, single values: string, number, boolean, null, undefined, bigint, symbol.
  • Reference types — bigger, more complex structures: object, array, function.
🧺 Real-life analogy

Primitive types are like a single item — an apple, a coin, a "yes." Reference types are like a basket that holds several items together — the basket itself is one thing, but it contains many.

Why Do Types Matter?

JavaScript decides which operations make sense based on type. You can add two numbers, but adding a number and a boolean does something unexpected unless you understand type conversion (its own lesson). Knowing types helps you predict what your code will actually do.

The Primitive Types

const name = "Maya";        // string — text, in quotes
const age = 27;             // number — any numeric value
const isStudent = false;    // boolean — true or false
let pet;                       // undefined — declared, no value yet
const car = null;            // null — deliberately "nothing"
const big = 123456789012345678901234567890n; // bigint — huge whole numbers
const id = Symbol("id");   // symbol — a guaranteed-unique value

string

Text, wrapped in single quotes ', double quotes ", or backticks `.

number

Any numeric value — whole or decimal, positive or negative. JavaScript has only one number type (unlike some languages that separate integers from decimals).

boolean

Exactly two possible values: true or false. Used for yes/no, on/off decisions.

undefined vs null — the two "nothings"

These trip up almost every beginner, so here's the clean distinction:

ValueMeaning
undefinedJavaScript set this automatically — "nobody has assigned a value yet"
nullA person deliberately wrote this — "I am intentionally saying there's nothing here"

bigint

For whole numbers too large for the regular number type to represent safely (bigger than about 9 quadrillion). Written with an n at the end: 900719925124740999n.

symbol

Creates a value that is always unique — even two symbols created with the same description are never equal. Mostly used internally, for advanced object features.

Reference Types

const user = { name: "Maya", age: 27 }; // object
const colors = ["red", "blue"];         // array (a special kind of object)
function greet() { return "hi"; }      // function (also a special kind of object)

The key difference: primitives are copied by value (each variable gets its own independent copy), while objects/arrays are copied by reference (the variable stores a pointer to the same shared data).

let a = 5;
let b = a;   // b gets its own copy of 5
b = 10;      // a is still 5

let obj1 = { count: 5 };
let obj2 = obj1; // obj2 points to the SAME object
obj2.count = 10;  // obj1.count is now ALSO 10!

Checking a Type: typeof

typeof "hello";   // "string"
typeof 42;        // "number"
typeof true;      // "boolean"
typeof undefined; // "undefined"
typeof null;      // "object" — a famous, decades-old JavaScript bug

Common Mistakes

⚠ Watch out for
  • typeof null returns "object", not "null" — a known quirk kept for backward compatibility.
  • Confusing null and undefined in comparisons — null == undefined is true, but null === undefined is false.
  • Forgetting that copying an object copies the reference, not the data — changing the "copy" changes the original too.

Best Practices

  • Use null when you intentionally want "no value"; let undefined mean "not set yet."
  • Use typeof or Array.isArray() to check types defensively when working with unpredictable data.
  • Prefer === over == to avoid type-conversion surprises (see Type Conversion).

Performance Notes

Primitive values are generally cheaper to copy and compare than objects, since objects require the engine to follow a reference rather than compare a simple value directly.

Browser Compatibility

All primitive types except bigint have been supported forever. bigint is a newer addition (2020) and is supported in all current major browsers.

Interview Questions

What's the difference between primitive and reference types?

Primitives store a single, immutable value and are copied by value. Reference types (objects, arrays, functions) are copied by reference, meaning two variables can point to the same underlying data.

Why does typeof null return "object"?

It's a long-standing bug from JavaScript's original 1995 implementation that was never fixed, because doing so would break too much existing code on the web.

✏️ Exercise

Declare one variable of each primitive type and log typeof for each one to confirm what JavaScript reports.

🧠 Quiz

1. Is an array a primitive or reference type?

Reference type — arrays are a special kind of object.

2. What does typeof undefinedVariable return if the variable was declared but never assigned a value?

"undefined".

🚀 Mini Challenge

Create an object original with a property, assign it to a new variable copy, change a property on copy, then log original to prove they're the same object in memory.

Summary

  • JavaScript has 7 primitive types and reference types like objects, arrays, and functions.
  • Primitives copy by value; objects copy by reference.
  • typeof tells you a value's type, with one famous exception: null.

Related Topics

MDN reference: developer.mozilla.org → JavaScript → Data types (placeholder — add live link)