typeof works for most values but says null and arrays are both "object". Learn the reliable checks for arrays, strings, numbers, null, undefined and plain objects.
The short answer
#Use typeof for primitives such as strings, numbers and booleans, Array.isArray for arrays, and a direct comparison for null. Do not rely on typeof alone, because it returns "object" for both null and arrays.
_10typeof 'hello' === 'string'; // true_10Array.isArray([1, 2, 3]); // true_10value === null; // true only for null_10_10typeof null; // "object"_10typeof []; // "object"
Why it happens
#typeof returns one of a small set of strings: "undefined", "boolean", "number", "bigint", "string", "symbol", "function" and "object". Anything that is not a primitive or a function counts as "object", so arrays, dates, maps and plain objects all look the same.
typeof null returning "object" is a bug from the first version of JavaScript. Changing it would break existing websites, so it stays. That is why null needs its own check.
How to fix it
#Check primitives with typeof
For strings, numbers, booleans, bigints, symbols and undefined, typeof is reliable and works even on variables that were never declared.
_10if (typeof input === 'string') {_10 input.trim();_10}_10_10if (typeof count === 'number' && Number.isFinite(count)) {_10 // a real number, not NaN or Infinity_10}
typeof NaN is "number", so add Number.isFinite or Number.isNaN when that matters.
Check arrays with Array.isArray
Array.isArray is the one reliable array test. It also works for arrays created in another frame or realm, where instanceof Array gives the wrong answer.
_10function toList(value) {_10 return Array.isArray(value) ? value : [value];_10}_10_10toList('a'); // ["a"]_10toList(['a', 'b']); // ["a", "b"]
Check null and undefined directly
Compare with === for one or the other, or use == null to catch both.
_10value === null; // only null_10value === undefined; // only undefined_10value == null; // null or undefined_10_10const name = user.name ?? 'Anonymous'; // default only for null or undefined
Check for a plain object
To know you have an object you can read keys from, exclude null and arrays.
_10function isPlainObject(value) {_10 if (typeof value !== 'object' || value === null) return false;_10 const proto = Object.getPrototypeOf(value);_10 return proto === Object.prototype || proto === null;_10}_10_10isPlainObject({ a: 1 }); // true_10isPlainObject([]); // false_10isPlainObject(new Date()); // false
Use instanceof or a tag for built-in objects
For dates, maps, errors and class instances, instanceof is usually enough. Object.prototype.toString gives a tag string when you need something that works across realms.
_10value instanceof Date; // true for dates_10Object.prototype.toString.call(new Map()); // "[object Map]"
In TypeScript, wrap these checks in type guard functions so the compiler narrows the type for you after the check.
Common pitfalls
#- Writing
typeof value === 'object'and assuming it is safe to read properties.nullpasses the check and then throws. - Using
typeof value === 'array'. That string never comes back fromtypeof, so the check is always false. - Checking strings with
instanceof String. String literals are primitives, so it isfalsefor'hello'. Usetypeof. - Using
!valueto test for missing values. It also catches0,''andfalse, which are often valid. Use== nullor??instead. - Forgetting that
typeofa function is"function", not"object", even though functions are objects.
Related reading
#Was this resource helpful?