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 typeof helps 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)

TypeExampleNotes
number3, -0.5, NaN, InfinityAll numbers are IEEE-754 doubles — no separate int type in normal code
string"hello", 'hi', `tick`Immutable — methods return new strings
booleantrue, falseResult of comparisons and logic
nullnullIntentional “no value”
undefinedundefinedVariable declared but not assigned, or missing property
bigint123nArbitrary-precision integers (specialized)
symbolSymbol("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

nullundefined
Typical meaning“I explicitly set this to nothing”“Not set yet” / “missing”
Common sourceYour 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" - 14). Prefer === and !== (strict equality) to avoid surprise coercions in beginner code.


6. Key takeaways

  1. number, string, boolean, null, undefined are the core primitives you use daily.
  2. Objects (including arrays) are mutable and assigned by reference.
  3. typeof null is "object" — memorize the quirk for interviews and debugging.

Explain-It Challenge

Explain without notes:

  1. What does immutable mean for a string, and give a tiny example.
  2. Why does changing b in const a = {}; const b = a; b.x = 1 also affect a?
  3. 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 →