typeof vs instanceof
typeof valuereturns a string naming the value’s basic type. Best for primitives.object instanceof Constructorreturns a boolean: isConstructor.prototypeanywhere in the object’s prototype chain? Best for class instances.
typeof 'hello' // 'string'
typeof 42 // 'number'
[] instanceof Array // true
new Date() instanceof Date // true
1. What typeof returns
| Value | typeof |
|---|---|
'text' |
'string' |
42, NaN |
'number' |
10n |
'bigint' |
true |
'boolean' |
undefined |
'undefined' |
Symbol() |
'symbol' |
function () {}, class A {} |
'function' |
{}, [], new Date(), null |
'object' |
Two results to remember:
typeof null === 'object'. A bug from the first version of JavaScript that can’t be fixed without breaking the web. Check withvalue === null.typeof []is'object'.typeofcan’t tell arrays, dates, maps and plain objects apart.
typeof is also safe on names that were never declared: typeof notDefined returns 'undefined' instead of throwing.
2. How instanceof works
It walks the prototype chain.
class Animal {}
class Dog extends Animal {}
const d = new Dog();
d instanceof Dog // true
d instanceof Animal // true
d instanceof Object // true
It doesn’t work on primitives:
'hello' instanceof String // false: a primitive, not an object
new String('hello') instanceof String // true
3. Where instanceof fails
- Across realms. Each iframe or worker has its own
Array,Errorand so on. An array created in an iframe is notinstanceofthe parent window’sArray. This is whyArray.isArrayexists. - Objects without a prototype.
Object.create(null) instanceof Objectisfalse. - Duplicate packages. Two copies of a library in one bundle define two different classes, so
instanceofcan fail for an object that looks identical. - It can be customised. A class can define
Symbol.hasInstanceand return anything.
4. Reliable checks
Array.isArray(value) // arrays, across realms
value === null // null
Number.isNaN(value) // NaN
typeof value === 'function' // callable
value !== null && typeof value === 'object' // any object, including arrays
For a precise built-in type, use the internal tag:
const typeOf = (v) => Object.prototype.toString.call(v).slice(8, -1);
typeOf(null) // 'Null'
typeOf([]) // 'Array'
typeOf(new Date()) // 'Date'
typeOf(/x/) // 'RegExp'
typeOf(new Map()) // 'Map'
Building this is a common exercise: precise type of.
5. In TypeScript
Both operators narrow types, which is why they still matter in typed code:
function format(value: string | number | Date) {
if (typeof value === 'string') return value.trim();
if (value instanceof Date) return value.toISOString();
return value.toFixed(2); // number
}
For objects that came from JSON there’s no class to test, so check a property instead ('kind' in value, or a discriminant field).
Choosing a check
| Checking for | Use |
|---|---|
| String, number, boolean, bigint, symbol, undefined | typeof |
| Function | typeof fn === 'function' |
null |
value === null |
| Array | Array.isArray(value) |
| Instance of your own class | instanceof |
Error in a catch block |
err instanceof Error (and check err.name for cross-realm cases) |
| Built-in type, reliably | Object.prototype.toString.call(value) |
| Shape of plain data | Property checks or a schema validator |
Follow-up questions to expect
- “Why is
typeof null‘object’?” A historical bug kept for compatibility. - “How do you check for a plain object?”
value !== null && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype(allow anullprototype if you need to). - “What does
typeof NaNreturn?”'number'. - “How does
instanceofrelate to prototypes?” It’s a prototype-chain lookup. See prototypal inheritance.
