To find the data type of a value in JavaScript, use the typeof operator for primitives and functions, Array.isArray() for arrays, and instanceof or Object.prototype.toString.call() for other objects. typeof is fast but has quirks: it returns "object" for null and for arrays.
Knowing which tool fits which type avoids a whole class of bugs when you validate input or debug unexpected values.
typeof returns a string naming the type of its operand. It never throws, even for an undeclared variable.
JavaScript has seven primitive types (string, number, bigint, boolean, undefined, symbol and null) plus objects. Functions are objects, but typeof reports them as "function".
typeof null returns "object". This is a bug from the first version of the language that cannot be fixed without breaking existing websites. Check for null explicitly with value === null.
Because typeof reports arrays as "object", use Array.isArray(). It also works across iframes and realms, where instanceof Array can fail.
instanceof tests whether a constructor's prototype appears in an object's prototype chain. It works for classes and built-in objects, but not for primitives.
Avoid wrapper objects such as new String("x") in real code. Use primitive strings and numbers instead.
For a reliable name of any built-in type, call Object.prototype.toString and read the tag.
The result is the part after [object in a string such as [object Array]. A class can change the tag through Symbol.toStringTag, so treat it as a hint, not proof, for custom objects.
These small helpers cover the most common needs.
Note that typeof NaN is "number", so use Number.isNaN() or Number.isFinite() when you need a real number.
| Value to check | Best method |
|---|---|
| string, number, boolean, bigint, symbol, undefined | typeof |
| null | value === null |
| array | Array.isArray(value) |
| function | typeof value === "function" |
| instance of your own class | value instanceof ClassName |
| Date, Map, Set, RegExp, Promise | instanceof or Object.prototype.toString.call() |
| plain object | prototype check, as in isPlainObject above |
typeof also works as a type guard in TypeScript, which narrows the type inside the branch.
TypeScript checks types at compile time only. Runtime checks like the ones above are still needed for data from APIs, forms and storage.
It is a historical bug in the original JavaScript engine that stayed for backward compatibility. Always compare with value === null.
Use typeof value === "undefined" when the variable might not be declared, or value === undefined when you know it exists.
Use typeof value === "number" and add Number.isFinite(value) to exclude NaN and Infinity.
typeof returns a string for the primitive type, while instanceof checks the prototype chain of an object against a constructor.
Use typeof for primitives and functions, === null for null, Array.isArray() for arrays, and instanceof or Object.prototype.toString.call() for objects. Remember that typeof null and typeof [] both return "object", and validate external data at runtime.