BonsaiDen/JavaScript-Garden

notes about the typeof operator

lewisje opened this issue · 0 comments

It looks odd that in the Type column, only "number" is in orange, rather than red.

I forget when this happened, but Nitro and V8 now return "object" instead of "function" for regular expressions.

Also, it should be noted that typeof null === "object"; and the spec authors have not been able to fix it without breaking existing code, and it might be noted that in older versions of IE, certain host objects can have the typeof value "unknown" and apparently "date" (although I've never figured out what values have that typeof value), even though MSDN does not mention these; also in those older versions of IE, some DOM methods have the typeof value "object" and do not have the Function.prototype methods, even though, as callable objects, they are functions (as with the previous note, this might complicate things unnecessarily).

A more minor issue is that typeof also returns "undefined" for the undefined value, not just for undeclared variables; an oddity from the HTML5 spec is that if document.all is implemented (for compatibility with oldIE), it must also have the typeof value "undefined", and an ES6-specific change is that if typeof is used on a let- or const-declared variable inside its scope, but before the declaration is reached (that is, in the Temporal Dead Zone, or TDZ), it will throw a ReferenceError.

Finally, another ES6-specific addition is that symbols have the typeof value "symbol" and the [[Class]] property "Symbol"; because the Symbol constructor may not be constructor-called, a Symbol wrapper object would be created with new Object(Symbol()), and as expected, its [[Class]] property is "Symbol" and its typeof value is "object".