How do you check a variable's type?
By FrontendPro Editorial Team Updated 9/26/2026
Answer
typeof handles primitives, but it has two well-known holes, so it is not a complete answer on its own.
typeof "hi"; // "string"
typeof 42; // "number"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof Symbol(); // "symbol"
typeof 10n; // "bigint"
typeof (() => {}); // "function"
typeof null; // "object" ← a bug kept for compatibility
typeof []; // "object" ← arrays are objects
Filling the gaps
value === null; // the only reliable null check
Array.isArray(value); // the correct array check
Array.isArray also works across iframes, where instanceof Array fails because each frame has its own Array constructor.
Distinguishing built-in objects
typeof says "object" for a Date, a Map and a plain object alike. When you need to tell them apart:
const typeOf = (v) => Object.prototype.toString.call(v).slice(8, -1).toLowerCase();
typeOf(null); // "null"
typeOf([]); // "array"
typeOf(new Date()); // "date"
typeOf(new Map()); // "map"
typeOf(/x/); // "regexp"
instanceof for your own classes
err instanceof ValidationError; // true for subclasses too
It checks the prototype chain, so it is right for class hierarchies and wrong for primitives — "hi" instanceof String is false.
Two practical notes
typeof is the only operator that does not throw on an undeclared variable, which makes typeof maybeGlobal !== "undefined" a safe existence check — except inside a temporal dead zone, where it throws like anything else.
And NaN is a number: use Number.isNaN(v) to detect it, and Number.isFinite(v) to reject NaN and both infinities at once.
