Sub topic 3
Inspecting Types with typeof
This chapter assumes you know the seven primitive types and how objects differ from primitives. You should also be able to run code in a browser console or in Node.js.
The working model
typeof is a unary operator. It returns a string that names the type of its operand. There are eight possible results, and they do not line up one-to-one with the eight language types. Two of them break the pattern:
nullreports"object".- Functions report
"function", even though functions are objects.
console.log(typeof 42); // "number"
console.log(typeof 10n); // "bigint"
console.log(typeof "hi"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof Symbol("id")); // "symbol"
console.log(typeof null); // "object" (a long-standing quirk)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function () {}); // "function"Why typeof null is "object"
The first JavaScript engine stored each value with a small type tag. The tag for objects was 0, and null was represented as the null pointer, which was all zero bits. typeof read that zero tag as "object". Fixing this would break existing websites, so the language specification now requires the quirk.
Common errors
Treating "object" as "a usable object"
A check like typeof x === "object" lets null through, and reading a property on null throws a TypeError. Add an explicit null check:
function isPlainObjectLike(x) {
return typeof x === "object" && x !== null;
}
console.log(isPlainObjectLike(null)); // false
console.log(isPlainObjectLike({})); // trueExpecting typeof to detect arrays
Arrays, dates, regular expressions, and maps all report "object". typeof only tells you that a value is an object. It cannot tell you which kind. A later chapter covers the tools for that, such as Array.isArray.
Forgetting about wrapper objects
typeof new String("a") returns "object", while typeof "a" returns "string". A value created with new is always an object.
A useful property: undeclared variables
typeof is the only way to refer to an undeclared variable without getting a ReferenceError:
console.log(typeof notDeclaredAnywhere); // "undefined"This is why older code used typeof window !== "undefined" to detect a browser environment. Variables declared with let or const are an exception. Using typeof on one of them before its declaration line runs still throws, because the variable is in the temporal dead zone.
Exercise
What do typeof typeof 42 and typeof NaN return, and why?
Show answer
Both return "string" and "number" respectively. typeof 42 evaluates to the string "number", and typeof applied to any string gives "string". NaN is a value of the Number type, so typeof NaN is "number".
Exercise
A function guards input with if (typeof config === "object") { use(config.port); }. Which input still crashes it, and how do you fix the guard?
Show answer
null crashes it, because typeof null is "object" and reading config.port on null throws a TypeError. Change the guard to typeof config === "object" && config !== null.