JavaScript gives you three built-in ways to check a type and none of them works for every case. typeof is fast but coarse and famously wrong about null. instanceof works on classes but fails across frames. Array.isArray is the only reliable array check. This guide covers which to use when, and a set of small helpers that cover the rest.
typeof#
typeof "hello" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof Symbol() // "symbol"
typeof 10n // "bigint"
typeof function(){} // "function"
typeof null // "object" <- a bug, kept for compatibility
typeof [] // "object"
typeof {} // "object"
typeof new Date() // "object"
Useful for primitives, useless for distinguishing kinds of object. The null result is a bug from the first version of JavaScript that was never fixed because too much code depends on it.
// Wrong - null passes
if (typeof value === "object") { value.name }
// Right
if (value !== null && typeof value === "object") { value.name }
One genuinely useful property: typeof does not throw on an undeclared variable, which nothing else can claim.
if (typeof someGlobal !== "undefined") { } // safe
if (someGlobal !== undefined) { } // ReferenceError
instanceof#
[] instanceof Array // true
new Date() instanceof Date // true
/x/ instanceof RegExp // true
class Dog {}
new Dog() instanceof Dog // true
"hello" instanceof String // false - a primitive, not an object
It walks the prototype chain, so it works with inheritance. Two limitations matter.
It does not work on primitives, as the last line shows. And it fails across execution contexts — an array from an iframe or a Node worker has a different Array constructor, so instanceof Array returns false for something that unmistakably is one.
Arrays#
Array.isArray([]) // true
Array.isArray("abc") // false
Array.isArray({length: 0}) // false
This is the correct check, always. It works across frames, where instanceof does not, and it is unambiguous, where typeof is not.
The reliable general check#
function typeOf(value) {
return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}
typeOf(null) // "null"
typeOf([]) // "array"
typeOf({}) // "object"
typeOf(new Date()) // "date"
typeOf(/x/) // "regexp"
typeOf(new Map()) // "map"
typeOf(NaN) // "number"
typeOf(() => {}) // "function"
Every built-in type gets a distinct answer, including null. It is slower than typeof and works across frames. Useful as a general-purpose helper; overkill for a single string check.
Plain objects#
function isPlainObject(value) {
if (typeOf(value) !== "object") return false;
const proto = Object.getPrototypeOf(value);
return proto === null || proto === Object.prototype;
}
isPlainObject({}) // true
isPlainObject(Object.create(null)) // true
isPlainObject(new Date()) // false
isPlainObject([]) // false
isPlainObject(new Dog()) // false
“Is this a configuration object rather than a class instance or an array” comes up constantly in library code, and there is no built-in for it.
Numbers#
typeof NaN // "number" - technically true, rarely helpful
NaN === NaN // false - the only value not equal to itself
Number.isNaN(NaN) // true
Number.isNaN("hello") // false - does not convert first
isNaN("hello") // true - the old global does convert
Number.isFinite(42) // true
Number.isFinite(Infinity) // false
Number.isInteger(4.0) // true
Number.isInteger(4.5) // false
Use the Number. versions. The old global isNaN converts its argument first, so it reports true for any non-numeric string — almost never what you meant.
function isValidNumber(value) {
return typeof value === "number" && Number.isFinite(value);
}
isValidNumber(42) // true
isValidNumber(NaN) // false
isValidNumber(Infinity) // false
isValidNumber("42") // false
Empty and missing#
// Not set at all
value == null // true for null and undefined only
// Distinguishing them
value === undefined
value === null
// A property that exists but is undefined
"key" in obj // true even if the value is undefined
obj.key !== undefined // false in that case
// Empty of content
function isEmpty(value) {
if (value == null) return true;
if (typeof value === "string" || Array.isArray(value)) return value.length === 0;
if (value instanceof Map || value instanceof Set) return value.size === 0;
if (typeOf(value) === "object") return Object.keys(value).length === 0;
return false;
}
Check at the boundary#
The practical answer is not to check types everywhere. Validate once, where untrusted data enters — an API response, a form, a file — and trust it internally.
function parseOrder(raw) {
if (!isPlainObject(raw)) throw new TypeError("Order must be an object");
const id = raw.id;
if (!Number.isInteger(id)) throw new TypeError("id must be an integer");
const items = raw.items;
if (!Array.isArray(items) || items.length === 0) {
throw new TypeError("items must be a non-empty array");
}
return { id, items, total: Number(raw.total) || 0 };
}
For anything beyond a handful of fields, a schema library does this better than hand-written checks:
import { z } from "zod";
const Order = z.object({
id: z.number().int(),
items: z.array(z.string()).min(1),
total: z.number().nonnegative(),
});
const order = Order.parse(raw); // throws with a precise message
Duck typing#
// Rather than checking the class, check for the ability you need
function isThenable(value) {
return value != null && typeof value.then === "function";
}
function isIterable(value) {
return value != null && typeof value[Symbol.iterator] === "function";
}
Often more useful than an exact type check, and it works with objects from other libraries and other frames.
When to move to TypeScript#
function total(items: { price: number; quantity: number }[]): number {
return items.reduce((sum, i) => sum + i.price * i.quantity, 0);
}
total("not an array"); // caught at compile time, before it runs
Runtime checks tell you about a problem when the line executes. TypeScript tells you while you type. It does not replace validation at the boundary — an API can still send you anything — but it removes most internal type mistakes entirely.
Questions people ask#
Why does typeof null return “object”?
A bug in the original implementation, where values were tagged by type and null had the object tag. Fixing it would break existing code, so it stays.
Should I use == or ===?
=== everywhere, with one accepted exception: value == null as a concise check for null or undefined together.
Is instanceof unreliable?
It is reliable within one execution context and for your own classes. It fails across frames and workers, which is why Array.isArray exists.
Do I still need runtime checks with TypeScript?
Yes, at the boundaries. TypeScript types vanish at compile time, so data arriving from a network or a file is unchecked. Validate it once on the way in.
Where to go next#
- JavaScript basics — types and truthiness in context.
- Arrays and objects in JavaScript — what you are usually checking.
- The JavaScript fetch API — where untrusted data arrives.