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.

JavascriptData Integrity

Code

javascriptsnippet
20 lines
// 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: object

How 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

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`.