JavaScript typeof and instanceof for undefined Checks
typeof and instanceof are tools for type checks. Here is how they handle undefined and what to use instead.
JavaScript typeof and instanceof for undefined Checks
typeof and instanceof are the two main type-checking operators in JavaScript. They behave differently with undefined.
typeof and undefined
typeof undefined; // "undefined" typeof x; // "undefined" (even if x was never declared, no error) `` `typeof` is the **safe** way to check for undeclared variables because it does not throw a `ReferenceError`: ```js if (typeof someGlobal !== "undefined") { someGlobal.doSomething(); } `` This is the standard pattern for feature detection (checking if a global API exists). ### `typeof` Returns for Common Values ```js typeof undefined; // "undefined" typeof null; // "object" (historical bug) typeof 5; // "number" typeof "hi"; // "string" typeof true; // "boolean" typeof {}; // "object" typeof []; // "object" (arrays are objects) typeof (() => {}); // "function" typeof Symbol(); // "symbol" typeof 5n; // "bigint" `` Note that `typeof null` is `"object"` (a bug that cannot be fixed), and `typeof []` is `"object"` (use `Array.isArray` for arrays). ### `instanceof` and `undefined` `instanceof` checks the prototype chain. It does not work with `undefined`: ```js undefined instanceof Object; // false null instanceof Object; // false `` `instanceof` is for objects: ```js [] instanceof Array; // true new Date() instanceof Date; // true class Foo {} new Foo() instanceof Foo; // true `` `instanceof` throws a `TypeError` if the right side is not callable: ```js 5 instanceof 5; // TypeError: Right-hand side of instanceof is not callable `` ### When to Use Which - **`typeof x === "undefined"`**: check if a variable exists (may be undeclared) or is the value `undefined`. - **`x === undefined`**: check if a declared variable is `undefined` (more precise, but throws if `x` is undeclared). - **`Array.isArray(x)`**: check for arrays (not `typeof` or `instanceof`). - **`x instanceof Class`**: check if an object is an instance of a class/constructor. ### The Takeaway `typeof` returns `"undefined"` for both `undefined` and undeclared variables, and does not throw. Use it for feature detection. `instanceof` checks prototype chains for objects and is not useful for `undefined` or `null`. Use `=== undefined` for declared variables and `Array.isArray` for arrays.
'undefined'. typeof undefined is 'undefined', and typeof an undeclared variable is also 'undefined' without throwing a ReferenceError. This makes typeof safe for feature detection.
No. undefined instanceof Object is false, and null instanceof Object is false. instanceof checks the prototype chain, which undefined and null do not have. Use instanceof only for objects.
It is a historical bug from the original implementation. null was represented with a type tag of 0, which the typeof check interpreted as object. It cannot be fixed without breaking existing code.
Use Array.isArray(x). typeof [] returns 'object', and [] instanceof Array can fail across frames/iframes. Array.isArray is the reliable way.
Use typeof x !== 'undefined' when x might be undeclared (no ReferenceError). Use x === undefined when x is definitely declared and you want a precise value check.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

