typeof null Reports object, Compare With ===
typeof classifies null as object because of a legacy type tag, so branches that trust typeof must check value === null separately to catch unset values.
Code
// typeof reports a value's type tag, and null's tag has been "object"
// since JavaScript 1.0 -- kept forever for backward compatibility.
const settings = {
theme: "dark", // string
retries: 3, // number
lastImport: null, // "not set yet" -- still typed as object
rules: ["a", "b"], // object
};
console.log(typeof settings.theme); // string
console.log(typeof settings.retries); // number
console.log(typeof settings.lastImport); // object
console.log(typeof settings.rules); // object
console.log(typeof settings.missing); // undefined
// Distinguish null by identity comparison, not by typeof
for (const [key, value] of Object.entries(settings)) {
const kind = value === null ? "null" : typeof value;
console.log(`${key}: ${kind}`);
}Output
Captured from a sandboxed run of this snippet in the build pipeline.
string
number
object
object
undefined
theme: string
retries: number
lastImport: null
rules: objectHow the snippet works
for (const [key, value] of Object.entries(settings)) is the loop that carries the per-item work.
Top-level bindings settings hold the demo state — nothing sits inside a function scope.
The top-level object literal pins theme, retries, lastImport as plain values — no derivation, no imports behind them.
Statements that matter
lastImport: null, // "not set yet" -- still typed as object
rules: ["a", "b"], // object
};
const kind = value === null ? "null" : typeof value;
Interactive Tools for This Topic
SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Text Diff ComparisonCompare two text blocks line by line and visualize the differences with added, removed, and unchanged lines highlighted. Useful for reviewing changes and patches.
FAQ
- Is a package install part of the setup?
- No. Installation is unnecessary because only built-in APIs are used. Its state lives in module-level bindings: `settings`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.