Episode 1 — Fundamentals / 1.13 — Introduction to JavaScript
1.13.g — Data Types: Primitives & Non-Primitives
In one sentence: JavaScript values are either primitives (immutable “atoms” stored by value in assignments) or objects (mutable collections stored by reference) — and
typeofhelps you inspect types at runtime (with a few quirks).
Navigation: ← 1.13.f — Naming & Clean Code · 1.13.h — Mini Practice Programs →
1. Primitives (this section’s focus)
| Type | Example | Notes |
|---|---|---|
number | 3, -0.5, NaN, Infinity | All numbers are IEEE-754 doubles — no separate int type in normal code |
string | "hello", 'hi', `tick` | Immutable — methods return new strings |
boolean | true, false | Result of comparisons and logic |
null | null | Intentional “no value” |
undefined | undefined | Variable declared but not assigned, or missing property |
bigint | 123n | Arbitrary-precision integers (specialized) |
symbol | Symbol("id") | Unique keys (advanced) |
Primitive values themselves are immutable: "ab".toUpperCase() returns a new string; it does not mutate "ab".
2. Non-primitive: objects (reference type)
Object (including plain objects {}, arrays [], functions, dates, etc.) — stored by reference:
const a = { count: 1 };
const b = a;
b.count = 2;
console.log(a.count); // 2 — a and b point to the same object
Two variables can alias the same object in memory.
3. null vs undefined
null | undefined | |
|---|---|---|
| Typical meaning | “I explicitly set this to nothing” | “Not set yet” / “missing” |
| Common source | Your code or APIs documenting “empty” | Unassigned variable, missing object property |
Both are falsy in boolean contexts (if (!value)).
4. typeof operator
typeof 42; // "number"
typeof "42"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" ← long-standing spec quirk; use === null to test null
typeof {}; // "object"
typeof []; // "object" ← arrays are objects; use Array.isArray(x)
Use typeof for quick checks; combine with === null or Array.isArray when precision matters.
5. Type coercion (preview)
JS will coerce types in loose operations ("5" - 1 → 4). Prefer === and !== (strict equality) to avoid surprise coercions in beginner code.
6. Key takeaways
number,string,boolean,null,undefinedare the core primitives you use daily.- Objects (including arrays) are mutable and assigned by reference.
typeof nullis"object"— memorize the quirk for interviews and debugging.
Explain-It Challenge
Explain without notes:
- What does immutable mean for a string, and give a tiny example.
- Why does changing
binconst a = {}; const b = a; b.x = 1also affecta? - Why is
typeof []"object", and what should you use instead to detect an array?
Navigation: ← 1.13.f — Naming & Clean Code · 1.13.h — Mini Practice Programs →