Sub topic 15
The document.all Oddity
This chapter assumes you already know the eight falsy values, how ||, && and ?? choose an operand, and how optional chaining stops at null or undefined. Earlier chapters said that every object is truthy. Browsers have exactly one exception to that rule, document.all. You will almost never use it. It still matters because it shows where the truthiness rules come from, and it explains why the language specification needs a special rule to describe them.
What document.all Is
document.all is a legacy browser property. It returns a collection of every element in the page. Early versions of Internet Explorer introduced it before the standard DOM methods such as document.getElementById existed.
The value is a real object of type HTMLAllCollection. It has a length, you can index it like an array, and you can look up elements by id:
// Run this in a browser's developer console on any web page.
console.log(document.all.length > 0); // true
console.log(document.all[0].tagName); // "HTML"
console.log(document.all instanceof HTMLAllCollection); // trueNode.js has no document, so every example in this chapter has to run in a browser console.
Why an Object Became Falsy
Old websites often detected Internet Explorer like this:
if (document.all) {
// Internet Explorer-only code path
} else {
// Code for other browsers
}Other browsers then faced a problem. Many pages also used document.all to find elements, so a browser had to provide it to make those pages work. If the property simply existed, though, the IE check above would pass, and those browsers would run code written only for Internet Explorer. That code often broke.
The browsers found a compromise. document.all exists and works when you use it, but it reports itself as falsy and as "undefined" under typeof. Feature checks fail, and actual usage succeeds. The HTML and ECMAScript specifications later standardized this behavior so that every browser does the same thing.
How ToBoolean Handles It
Every boolean context covered in earlier chapters, such as if, while, the ternary operator, !, Boolean(), || and &&, goes through the specification's ToBoolean operation. That operation has a single extra branch for objects.
So the rule "all objects are truthy" really means "all objects except the one carrying [[IsHTMLDDA]]".
// Browser console
console.log(Boolean(document.all)); // false
console.log(!!document.all); // false
console.log(!document.all); // true
if (document.all) {
console.log("never logged");
} else {
console.log("document.all is falsy"); // logged
}The Full Set of Special Behaviors
The [[IsHTMLDDA]] slot changes three things in the language: ToBoolean, typeof, and loose equality with null and undefined. Nothing else changes. Strict equality, property access, optional chaining and ?? all treat document.all as the ordinary object it is.
// Browser console
console.log(typeof document.all); // "undefined"
console.log(document.all == undefined); // true
console.log(document.all == null); // true
console.log(document.all === undefined); // false
console.log(document.all === null); // false
console.log(document.all == false); // falseThe == false result follows the rules from the previous chapter. false is first converted to 0. document.all is then converted to a primitive, its string form "[object HTMLAllCollection]", which becomes NaN as a number. NaN == 0 is false. The value is falsy and also not == false, another reminder that truthiness and loose equality are separate rules.
| Operation | Result with document.all | Rule involved | Column | Column | Column | Column |
|---|---|---|---|---|---|---|
| `Boolean(document.all)` | `false` | ToBoolean special case | ||||
| `typeof document.all` | `"undefined"` | typeof special case | ||||
| `document.all == null` | `true` | Loose equality special case | ||||
| `document.all === undefined` | `false` | Strict equality, no special case | ||||
| `document.all \ | \ | "x"` | `"x"` | `\ | \ | ` uses ToBoolean |
| `document.all && "x"` | `document.all` | `&&` uses ToBoolean | ||||
| `document.all ?? "x"` | `document.all` | `??` checks for actual null or undefined | ||||
| `document.all?.length` | a number | `?.` checks for actual null or undefined |
Interaction with the Short-Circuit Operators
This is where the chapter ties back to the rest of the tutorial. || and && decide by truthiness, so they treat document.all as falsy. ?? and ?. decide by asking whether the value is null or undefined, and document.all is neither.
// Browser console
const viaOr = document.all || "fallback";
const viaAnd = document.all && "next";
const viaNullish = document.all ?? "fallback";
const viaChain = document.all?.length;
console.log(viaOr); // "fallback"
console.log(viaAnd === document.all); // true
console.log(viaNullish === document.all); // true
console.log(typeof viaChain); // "number"The same split shows up in logical assignment, because ||= and &&= follow || and &&, and ??= follows ??:
// Browser console
const box = { all: document.all };
box.all ??= "replaced";
console.log(box.all === document.all); // true, ?? saw a real object
box.all ||= "replaced";
console.log(box.all); // "replaced", || saw a falsy valueEarlier chapters gave a guideline: use ?? when only "missing" values should trigger a default. document.all is the one value for which || and ?? disagree even though it is an object, and ?? gives the correct answer that the object is present.
Edge Cases and Gotchas
typeof checks report it as missing
The usual guard typeof x === "undefined" returns true for document.all. Code that uses typeof to detect whether a feature exists will conclude that document.all is absent. That was the intended effect.
Loose null checks match it
The idiom x == null, which catches both null and undefined, also matches document.all. If you write a utility that must tell "missing" apart from "present but odd", use x === null || x === undefined or ??.
It only applies in browsers
In Node.js, Deno workers without a DOM, and other non-browser runtimes, document does not exist and no object carries [[IsHTMLDDA]]. In those environments, the eight-value falsy list is complete with no exceptions.
You cannot create your own
No JavaScript code can add [[IsHTMLDDA]] to an object. Proxies, classes, and Symbol.toPrimitive cannot make an object falsy. Defining valueOf or toPrimitive changes how the object converts to a number or string, but ToBoolean never calls those methods.
// Runs anywhere, including Node.js
const tricky = {
valueOf() { return 0; },
[Symbol.toPrimitive]() { return 0; },
};
console.log(Boolean(tricky)); // true, objects ignore conversion hooks here
console.log(tricky == 0); // true, loose equality does use themWhen This Knowledge Actually Matters
You are unlikely to meet document.all in application code. It turns up in a few specific places:
- Library code that walks arbitrary values, such as deep-clone, serialization, or type-checking utilities, can misclassify it as
undefined. - Test suites for JavaScript engines and tooling include it, because it is the only falsy object.
- Interview questions and quizzes use it as a trick question when they ask whether all objects are truthy.
For each of these, the decision rule is the same. If you need "is this value literally null or undefined?", use === or ??. If you need "would an if statement skip this?", use truthiness and accept that document.all counts as falsy.
Exercise
In a browser console, what do these three expressions return: document.all || 1, document.all ?? 1, and typeof document.all? Explain why the first two differ.
Show answer
document.all || 1 returns 1, document.all ?? 1 returns document.all itself, and typeof document.all returns "undefined". The || operator decides by ToBoolean, which treats document.all as falsy because of its [[IsHTMLDDA]] slot. The ?? operator only checks whether the value is actually null or undefined, and document.all is an object, so ?? keeps it.
Exercise
A utility function uses if (value == null) return "missing";. Which browser value returns "missing" even though it is a real object, and how would you rewrite the check so that only actual null and undefined count as missing?
Show answer
document.all returns "missing", because loose equality treats it as equal to null and undefined. Rewrite the check as if (value === null || value === undefined) return "missing";. Strict equality has no special case for document.all.
Exercise
Can you write a plain JavaScript object, without using document.all, for which Boolean(obj) is false? Justify your answer.
Show answer
No. ToBoolean returns true for every object unless it has the [[IsHTMLDDA]] internal slot, and only the host browser can attach that slot. Hooks such as valueOf and Symbol.toPrimitive affect numeric and string conversion, but ToBoolean never calls them.