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.
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:
| Value | Meaning |
|---|---|
undefined |
JavaScript set this automatically — "nobody has assigned a value yet" |
null |
A 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
Objects, arrays, and functions are reference types. They are covered in their own dedicated sections later. The key thing to know now: primitives copy by value (each variable gets its own independent copy), while reference types copy by reference (two variables can point to the same data). You'll see this in action when you reach the Objects and Arrays sections.
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
typeof has one unique superpower: you can use it on a
variable that has never been declared at all, and it returns
"undefined" instead of throwing a
ReferenceError. Every other way of accessing an
undeclared variable crashes. This makes typeof useful
for safely checking whether an optional global exists:
// Normal access — crashes if "myLib" was never declared:
if (myLib) { } // ❌ ReferenceError
// typeof — safe even if "myLib" doesn't exist:
if (typeof myLib !== "undefined") { } // ✅ no crash
typeof — Full Results Table
| Value | typeof result |
|---|---|
"hello" |
"string" |
42, NaN, Infinity |
"number" |
true / false |
"boolean" |
undefined |
"undefined" |
null |
"object" ← bug |
100n (BigInt) |
"bigint" |
Symbol() |
"symbol" |
function(){} |
"function" |
{}, [] |
"object" |
Use Array.isArray(val) to distinguish arrays from plain
objects, since both return "object" from
typeof.
You'll notice typeof function(){} returns
"function", not "object" — even though
functions are a kind of object. Functions are covered in their own
section. For now, just know they are a special case in the
typeof table above.
undefined vs Undeclared — Not the Same Thing
Beginners often treat these as identical, but they are different situations with different consequences.
| Situation | What it means | What happens if you use it |
|---|---|---|
Declared, no valuelet x;
|
Variable exists in memory, JavaScript set it to
undefined automatically
|
Works fine — you get undefined |
Undeclaredconsole.log(z)
where z was never written
|
Variable does not exist at all — JavaScript has never heard of it | ❌ ReferenceError: z is not defined |
let declared;
console.log(declared); // undefined — declared, just never assigned
console.log(typeof declared); // "undefined"
// console.log(neverDeclared); ❌ ReferenceError — it simply doesn't exist
console.log(typeof neverDeclared); // "undefined" — typeof is the ONE safe way to check
typeof is the only operation you can safely perform
on an undeclared variable without crashing. Everything else —
reading it, passing it to a function, using it in math — throws a
ReferenceError.
Why Does typeof null Return "object"?
This is one of JavaScript's most famous bugs. When the language was
first created in 1995, values were stored in memory with a small tag
indicating their type. The tag for objects was 000.
Unfortunately, null was represented internally as a
null pointer — whose bits also started with 000. So the
type-checking code incorrectly classified null as an
object.
By the time this was discovered, billions of web pages depended on
this behavior. Fixing it would have broken the entire web. So it was
kept — permanently — as a known bug. The correct way to check for
null is with strict equality:
typeof null; // "object" ← wrong, but kept forever
null === null; // true ✅ this is the correct check
value === null; // true only if value is exactly null
value == null; // true if value is null OR undefined (intentional pattern)
Real-World Examples
// Each primitive type used in a realistic context
const userName = "Maya"; // string
const userAge = 27; // number
const isPremium = true; // boolean
let phoneNumber = null; // null — intentionally not provided yet
let lastLogin; // undefined — not set yet
console.log(typeof userName); // "string"
console.log(typeof userAge); // "number"
console.log(typeof isPremium); // "boolean"
console.log(typeof phoneNumber); // "object" ← the famous bug!
console.log(typeof lastLogin); // "undefined"
Try It: Code Runner
Explore types with typeof. Try adding your own
variables and checking their types.
When you learn about objects and arrays, you'll discover that copying them can be tricky — there are "shallow" and "deep" copies with different behaviors. That's covered fully in the Objects section.
Common Mistakes
-
typeof nullreturns"object", not"null"— a known quirk kept for backward compatibility. -
Confusing
nullandundefinedin comparisons —null == undefinedistrue, butnull === undefinedisfalse. - Forgetting that copying an object copies the reference, not the data — changing the "copy" changes the original too.
Best Practices
-
Use
nullwhen you intentionally want "no value"; letundefinedmean "not set yet." -
Use
typeoforArray.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
Declare one variable of each primitive type (string,
number, boolean, null,
undefined) and log typeof for each.
Which one gives a surprising result?
Summary
- JavaScript has 7 primitive types and reference types like objects, arrays, and functions.
- Primitives copy by value; objects copy by reference.
-
typeoftells you a value's type, with one famous exception:null.
Related Topics
MDN reference: developer.mozilla.org → JavaScript → Data types (placeholder — add live link)