Learn JavaScript
❯ types.typeof

typeof & Type Checking in JavaScript

$concept

typeof is the quick type probe: "string", "number", "boolean", "undefined", "function", "bigint", "symbol" — all reliable, with two famous warts: typeof null === "object" (fossil bug) and every object/array/date reporting "object" (uselessly vague). It never throws, even on undeclared variables — the one safe existence check.

Real checking composes tools: Array.isArray for arrays, instanceof for class hierarchies (fragile across realms), constructor/prototype checks, and duck-typing (has a .then? treat as promise-like). Modern codebases add TypeScript or Zod schemas where checking truly matters.

$how it works
1

Reliable for primitives

typeof 1, typeof "s", typeof undefined, typeof BigInt(1), typeof Symbol() — all exact, all safe.

2

Vague for objects

typeof {} === typeof [] === "object" — reach for Array.isArray and instanceof past primitives.

3

null lies, undeclared is safe

typeof null says "object" (ignore it); typeof neverDeclared says "undefined" without throwing — unique among probes.

4

Compose real checks

Array.isArray, instanceof chains, duck-typing, and schema validators — typeof starts checks, rarely finishes them.

$example
console.log(typeof 42, typeof "s", typeof undefined);
console.log(typeof null, typeof [], Array.isArray([]));
Output
number string undefined
object object true
1Primitives report exactly — number, string, undefined.
2null misreports object (fossil), arrays report object (vague) — Array.isArray gives the true answer.
3The two lines map typeof's honest zone and its two famous blind spots.
$use cases
Quick primitive guards (typeof cb === "function") before calling
Safe undeclared-variable checks no other probe allows
Array detection exclusively through Array.isArray
Schema validation (Zod/Yup) where types truly gate logic

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.