Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How to Check if a Type Exists in JavaScript

JavaScript
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedOct 1, 2024
UpdatedNov 16, 2026
6 min read

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.

Table of Contents

  • Check whether a variable or global exists with typeof
  • Values returned by typeof
  • Check whether a class or constructor exists
  • Check for arrays, null and plain objects
  • Check whether a property exists on an object
  • Check the type of an existing value in TypeScript
  • Frequently Asked Questions
    • Why does typeof not throw for an undeclared variable?
    • What is the difference between typeof and instanceof?
    • How do I check that a value is neither null nor undefined?
    • Is typeof null really "object"?
  • Summary

Check whether a variable or global exists with typeof

Reading an undeclared identifier throws a ReferenceError. The typeof operator is the exception: applied to an undeclared name it returns "undefined" instead of throwing.

javascript
javascript
Loading syntax highlighting...

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.

javascript
javascript
Loading syntax highlighting...

Values returned by typeof

typeof returns one of eight strings.

Valuetypeof 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.

Check whether a class or constructor exists

typeof tells you a class is defined, and instanceof tells you whether a value was created by it.

javascript
javascript
Loading syntax highlighting...

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.

Check for arrays, null and plain objects

javascript
javascript
Loading syntax highlighting...

For the exact built-in tag of any value, Object.prototype.toString is reliable:

javascript
javascript
Loading syntax highlighting...

Check whether a property exists on an object

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.

javascript
javascript
Loading syntax highlighting...

Prefer Object.hasOwn(obj, key) over the older obj.hasOwnProperty(key), which fails for objects created with Object.create(null).

Check the type of an existing value in TypeScript

In TypeScript, narrowing with typeof and instanceof is understood by the compiler, so the checked branch gets the correct type.

typescript

Frequently Asked Questions

Why does typeof not throw for an undeclared variable?

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.

What is the difference between typeof and instanceof?

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.

How do I check that a value is neither null nor undefined?

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.

Is typeof null really "object"?

Yes. It is a bug from the first JavaScript implementation that was kept for backward compatibility, so test for null with === null explicitly.

Summary

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.

Share