typeof & Type Checking in JavaScript
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.
Reliable for primitives
typeof 1, typeof "s", typeof undefined, typeof BigInt(1), typeof Symbol() — all exact, all safe.
Vague for objects
typeof {} === typeof [] === "object" — reach for Array.isArray and instanceof past primitives.
null lies, undeclared is safe
typeof null says "object" (ignore it); typeof neverDeclared says "undefined" without throwing — unique among probes.
Compose real checks
Array.isArray, instanceof chains, duck-typing, and schema validators — typeof starts checks, rarely finishes them.
console.log(typeof 42, typeof "s", typeof undefined);console.log(typeof null, typeof [], Array.isArray([]));Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.