Use typeof to check whether a variable or a global name exists without risking a ReferenceError, and use instanceof, Array.isArray() or Object.hasOwn() to check whether a class, array or property exists. typeof someName === "undefined" is the only operator-based test that is safe for a name that was never declared.
"Type exists" can mean several things in JavaScript: a variable is declared, a global such as fetch is available, a class is defined, or a value has a certain type. This article covers each case with current syntax.
Reading an undeclared identifier throws a ReferenceError. The typeof operator is the exception: applied to an undeclared name it returns "undefined" instead of throwing.
Be aware of the temporal dead zone: a let, const or class binding that is declared later in the same scope throws a ReferenceError even with typeof if you read it before its declaration line runs.
typeof returns one of eight strings.
| Value | typeof result |
|---|---|
undefined | "undefined" |
true | "boolean" |
42 | "number" |
42n | "bigint" |
"text" | "string" |
Symbol("id") | "symbol" |
function () {} or a class | "function" |
{}, [], new Date() | "object" |
null | "object" (a long-standing historical quirk) |
Because null and arrays both report "object", typeof alone cannot separate them from plain objects.
typeof tells you a class is defined, and instanceof tells you whether a value was created by it.
instanceof walks the prototype chain, so it is also true for subclasses. It can give wrong results across realms, for example between an iframe and its parent page, because each realm has its own Array, Date and so on. For arrays use Array.isArray(), which works across realms.
For the exact built-in tag of any value, Object.prototype.toString is reliable:
Checking whether a property exists is different from checking its type. Use Object.hasOwn() (ES2022) for own properties and the in operator when inherited properties count.
Prefer Object.hasOwn(obj, key) over the older obj.hasOwnProperty(key), which fails for objects created with Object.create(null).
In TypeScript, narrowing with typeof and instanceof is understood by the compiler, so the checked branch gets the correct type.
The language defines typeof to return "undefined" for an unresolvable reference, which makes feature detection safe. Any other read of an undeclared name throws a ReferenceError.
typeof inspects the primitive type or whether a value is callable, and returns a string. instanceof checks whether a constructor's prototype appears in an object's prototype chain, and returns a boolean.
Use value != null, which is the one common use of loose equality, or value !== null && value !== undefined. Optional chaining (value?.prop) and nullish coalescing (value ?? fallback) cover most everyday cases.
Yes. It is a bug from the first JavaScript implementation that was kept for backward compatibility, so test for null with === null explicitly.
Use typeof name === "undefined" to test whether a name exists, instanceof for class membership, Array.isArray() for arrays and Object.hasOwn() for properties. Remember the null quirk and the temporal dead zone, and use TypeScript narrowing when you want the compiler to enforce the check.