Sub topic 15
The document.all Oddity
This chapter assumes you already know the full list of falsy values, how || and ?? choose a fallback, and how optional chaining stops at null or undefined. It covers the one exception to the rule that every object is truthy.
The One Falsy Object
Earlier chapters said that every object is truthy, including [] and {}. One browser object breaks that rule: document.all.
document.all is a legacy collection of every element on the page. Old versions of Internet Explorer introduced it, and pages from that era often used it to detect the browser:
if (document.all) {
// Old IE-only code path
} else {
// Standards-based code path
}Modern browsers had a problem. Many old pages still read elements through document.all, so removing it would break them. If it stayed truthy, though, those same pages would take the IE-only branch and break in a different way. Browsers settled on a compromise: document.all still works as a collection, but it pretends to be undefined in the checks that old detection code relied on.
The ECMAScript specification formalizes this in Annex B. Any object with an internal slot called [[IsHTMLDDA]] gets this behavior, and document.all is the only such object in practice.
Seeing It in a Browser
document.all exists only in browsers, so run this in the developer console of any web page. Node.js has no document object.
console.log(typeof document.all); // "undefined"
console.log(Boolean(document.all)); // false
console.log(!!document.all); // false
console.log(document.all == null); // true
console.log(document.all == undefined); // true
console.log(document.all === undefined); // false
console.log(document.all.length > 0); // true, it is a real collection
console.log(document.all || "fallback"); // "fallback"
console.log(document.all ?? "fallback"); // the HTMLAllCollection itself
console.log(document.all?.length); // a number, not undefinedThe last two lines are the subtle part. ?? and ?. check for null or undefined by type, without any conversion. document.all is an object, so it passes those checks and is used as a value. || asks whether the value is truthy, and document.all says no.
How Each Check Treats It
| Check | Result for `document.all` | Why | Column | Column | Column | Column |
|---|---|---|---|---|---|---|
| `if (document.all)` | Skips the block | `ToBoolean` returns `false` for `[[IsHTMLDDA]]` objects | ||||
| `typeof document.all` | `"undefined"` | Special-cased in the `typeof` operator | ||||
| `document.all == null` | `true` | Loose equality treats it like `undefined` | ||||
| `document.all === undefined` | `false` | Strict equality compares real identity | ||||
| `document.all \ | \ | x` | `x` | Falsy, so `\ | \ | ` moves on |
| `document.all ?? x` | `document.all` | Not actually `null` or `undefined` | ||||
| `document.all?.length` | A number | Optional chaining continues |
Should You Care?
In application code, you almost never will. Prefer document.querySelectorAll() and leave document.all alone. The case matters in three situations:
- You maintain very old code that still uses
document.allfor browser detection. - You write a library that inspects arbitrary values, such as a deep-equality or type-checking utility. A check like
value == nullwill misclassifydocument.all, whilevalue === null || value === undefinedwill not. - You want a precise answer to the claim "all objects are truthy." The accurate version is "all objects except
document.all."
Exercise
In a browser, what do document.all || "a" and document.all ?? "b" each evaluate to, and why do they differ?
Show answer
document.all || "a" evaluates to "a" because || tests truthiness and document.all is falsy. document.all ?? "b" evaluates to document.all itself because ?? only falls back when the value is actually null or undefined, and document.all is an object.
Exercise
A utility function uses if (value == null) return "missing";. What does it return for document.all, and how would you fix it?
Show answer
It returns "missing", because document.all loosely equals null. Use if (value === null || value === undefined) to treat document.all as the object it really is.