`typeof` and `instanceof`
typeof returns a string naming a value's type. instanceof asks whether an object has a given constructor's prototype on its prototype chain. They answer different questions and both have traps.
Detailed Explanation
typeof x is an operator, not a function. It never throws, even for an undeclared binding that is not in the temporal dead zone (typeof notDeclared is "undefined"). It returns one of "undefined", "boolean", "number", "string", "symbol", "bigint", "function", or "object".
The last two are the messy ones. Arrays, null, dates, regexps, maps, and plain objects all have typeof "object". Functions have typeof "function", which is a historical special case; they are still objects. typeof null === "object" is an old bug that the committee will not fix, because the web depends on it.
So typeof is good for primitives and for "is this callable?". It is a poor test for arrays (Array.isArray), dates (x instanceof Date or Object.prototype.toString.call(x)), or "is this a plain object".
instanceof evaluates Constructor.prototype and walks the left-hand value's [[Prototype]] chain looking for that object. [] instanceof Array is true. [] instanceof Object is true. A primitive is never instanceof anything: "hi" instanceof String is false, while new String("hi") instanceof String is true.
instanceof breaks across realms. An array created in an iframe, or passed from a vm context in Node, has a different Array.prototype. iframeArray instanceof Array is false. Array.isArray still returns true. The same problem hits Error, Date, and user classes loaded twice (two copies of the module, two prototype objects).
You can customise instanceof with Symbol.hasInstance on the right-hand side. x instanceof C then becomes C[Symbol.hasInstance](x). Built-ins do not need this; libraries that want x instanceof SomeType to accept duck-typed values sometimes use it.
What it costs you: trusting typeof for objects, and trusting instanceof for values that crossed a frame or a bundle duplicate. Prefer dedicated tests: Array.isArray, typeof x === "function", x == null, Number.isFinite(x), typeof x === "string". For a class you own, instanceof is fine inside one realm.
Reach for typeof on primitives. Reach for Array.isArray on lists. Reach for instanceof on a class in the same realm when the prototype chain is the contract.
Code Examples
console.log(typeof undefined); // => undefined
console.log(typeof true); // => boolean
console.log(typeof 1); // => number
console.log(typeof 1n); // => bigint
console.log(typeof "a"); // => string
console.log(typeof Symbol("k")); // => symbol
console.log(typeof function () {}); // => function
console.log(typeof {}); // => object
console.log(typeof []); // => object
console.log(typeof null); // => objectconsole.log([] instanceof Array); // => true
console.log([] instanceof Object); // => true
console.log("hi" instanceof String); // => false
console.log(new String("hi") instanceof String); // => true
console.log(Array.isArray([])); // => true
console.log(Array.isArray({ length: 1 })); // => falseclass Token {}
const t = new Token();
console.log(t instanceof Token); // => true
function isPlainObject(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
console.log(isPlainObject({})); // => true
console.log(isPlainObject([])); // => falseCommon Gotchas
`typeof null` is `"object"`
test `value === null` or `value == null`. Never use `typeof` to detect `null`.
`instanceof` fails across iframes and duplicate bundles
two `User` classes from two copies of a package do not match. Prefer a brand field or `Array.isArray` for built-ins.
Primitives are not `instanceof` their wrappers
`"s" instanceof String` is `false`. Use `typeof`.
`typeof` does not throw on an undeclared global
it does throw on a `let`/`const` still in the temporal dead zone.