Sub topic 17
Checking Types Reliably
This chapter assumes you know what typeof returns for each type, including typeof null. It also assumes you know that arrays are objects and that primitives can be wrapped in objects such as new String("a"). On top of that, it covers the checks that fill the gaps typeof leaves.
Where typeof Stops Being Enough
typeof works well for "string", "number", "bigint", "boolean", "undefined", "symbol", and "function". It fails in three places:
typeof nullis"object".- Arrays, dates, maps, and plain objects all report
"object". - Wrapper objects report
"object", sotypeof new String("a")is not"string".
Pick the check that answers your actual question.
const value = null;
console.log(value === null); // true: the direct null check
console.log(typeof value === "object" && value !== null); // false: a real object test
console.log(Array.isArray([])); // true
console.log(Array.isArray({ length: 0 })); // false: array-like is not an array
console.log(Object.prototype.toString.call(null)); // "[object Null]"
console.log(Object.prototype.toString.call([])); // "[object Array]"
console.log(Object.prototype.toString.call(new Date())); // "[object Date]"
console.log(new Date() instanceof Date); // trueThe Tools and What They Actually Test
| Check | What it tests | Weak spot |
|---|---|---|
| `typeof x` | The primitive type, or object versus function | `null`, arrays, and wrappers all give `"object"` |
| `x === null` | Exactly `null` | Nothing else |
| `x == null` | `null` or `undefined` | Only that one loose comparison is idiomatic |
| `Array.isArray(x)` | A real array, even one from another frame or realm | Only answers the array question |
| `x instanceof C` | Whether `C.prototype` is in `x`'s prototype chain | Fails across realms, and the prototype can be changed |
| `Object.prototype.toString.call(x)` | The internal tag, for example `"[object Date]"` | `Symbol.toStringTag` can fake it |
Common Errors
Trusting instanceof across realms
An array created in an iframe or a Node vm context has a different Array.prototype, so arr instanceof Array returns false for it. Array.isArray returns true for it. Always use Array.isArray to check for arrays.
Forgetting wrappers
const boxed = new String("hi");
console.log(typeof boxed); // "object"
console.log(boxed instanceof String); // true
console.log("hi" instanceof String); // false: primitives have no prototype chain
console.log(Object.prototype.toString.call(boxed)); // "[object String]"
console.log(Object.prototype.toString.call("hi")); // "[object String]"Object.prototype.toString gives the same tag for a primitive and its wrapper. If you need to tell them apart, use typeof.
Treating tags as proof
const fake = { [Symbol.toStringTag]: "Date" };
console.log(Object.prototype.toString.call(fake)); // "[object Date]"
console.log(fake instanceof Date); // falseTags are good for logging and quick checks, but any object can set its own tag. For a real date, combine instanceof Date with !Number.isNaN(d.getTime()) to reject invalid dates.
Choosing a Check
Exercise
Write a function isPlainObjectLike(x) that returns true for {} and { a: 1 }, and false for null, [], "text", and new Date(). Use only the checks from this chapter.
Show answer
function isPlainObjectLike(x) { return Object.prototype.toString.call(x) === "[object Object]"; } This gives "[object Null]" for null, "[object Array]" for [], "[object String]" for "text", and "[object Date]" for new Date(). Note that an object with a custom Symbol.toStringTag could still slip past this check.
Exercise
Why can arr instanceof Array be false while Array.isArray(arr) is true?
Show answer
arr was created in a different realm, such as an iframe or a vm context. Its prototype is that realm's Array.prototype, which instanceof does not find in the chain. Array.isArray checks the internal array marker, which works in every realm.