JavaScript’s `typeof` operator returns a string describing an operand’s basic type. It also lets you test whether a possibly undeclared global exists: `typeof ninjaName` returns `"undefined"` instead of throwing a `ReferenceError`. section 7.4 section 9.4.1
Check before initializing a global
Use `typeof ninjaName === "undefined"` before assigning to `globalThis.ninjaName`. For a declared global, this check is generally equivalent to `ninjaName === undefined`; for an undeclared global, the direct reference throws. section 7.4
const initializeGlobalName = () => {
if (typeof ninjaName === "undefined") {
globalThis.ninjaName = "Naruto";
}
}
A short example uses typeof to initialize a global only when it is undefined. section 7.4
This check does not avoid the Temporal Dead Zone for an uninitialized local variable: `typeof` can still throw a `ReferenceError`. Also, `typeof` reports class instances as `"object"`; it does not identify their class. section 7.4 section 9.4.1



