More JavaScript answers

by Danny
Published 2 min read

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.


_10
typeof 'hello' === 'string'; // true
_10
Array.isArray([1, 2, 3]); // true
_10
value === null; // true only for null
_10
_10
typeof null; // "object"
_10
typeof []; // "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.


_10
if (typeof input === 'string') {
_10
input.trim();
_10
}
_10
_10
if (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.


_10
function toList(value) {
_10
return Array.isArray(value) ? value : [value];
_10
}
_10
_10
toList('a'); // ["a"]
_10
toList(['a', 'b']); // ["a", "b"]

Check null and undefined directly

Compare with === for one or the other, or use == null to catch both.


_10
value === null; // only null
_10
value === undefined; // only undefined
_10
value == null; // null or undefined
_10
_10
const 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.


_10
function 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
_10
isPlainObject({ a: 1 }); // true
_10
isPlainObject([]); // false
_10
isPlainObject(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.


_10
value instanceof Date; // true for dates
_10
Object.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. null passes the check and then throws.
  • Using typeof value === 'array'. That string never comes back from typeof, so the check is always false.
  • Checking strings with instanceof String. String literals are primitives, so it is false for 'hello'. Use typeof.
  • Using !value to test for missing values. It also catches 0, '' and false, which are often valid. Use == null or ?? instead.
  • Forgetting that typeof a function is "function", not "object", even though functions are objects.
#

Was this resource helpful?