Does typeof type operator return an uppercase string?

I recently came across the following line of code:

var type = (typeof x).toLowerCase(); 

Note that in the above code, x will only be a string, number or undefined. I questioned this by pointing out that the specification (11.4.3) asserts the values ​​returned by the typeof operator, and all of them are already lowercase.

It is worth noting that the specification leaves the host objects free to return almost everything they like, so in this case you can get a string with some uppercase letters (I do not actually do this ever, but it is allowed). However, as already mentioned, in this case x is only a string, number or undefined.

My question is, do any typeof operator implementations ever return anything but a string string?

+4
source share
3 answers

Spidermonkey seems to return only these:

 "undefined" "object" "function" "string" "number" "boolean" NULL 

https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_GetTypeName

Same thing with V8:

 default: // For any kind of object not handled above, the spec rule for // host objects gives that it is okay to return "object" return isolate->heap()->object_symbol(); 

http://code.google.com/p/v8/source/browse/branches/bleeding_edge/src/runtime.cc#5245

I don’t know about MS, I think they do not use a custom type, but you never know with them.

There are six possible values ​​returned by typeof: "number", "string", "boolean", "object", "function" and "undefined."

http://msdn.microsoft.com/en-us/library/windows/apps/259s7zc1%28v=vs.94%29.aspx

+1
source

return values ​​for typeof statement

Undefined: "undefined"
Null: "object"
Boolean: "boolean"
Number: "number"
String: "string"
Object (native and does not implement Call): "object"
Object (native and implements Call): "function"
Object (host): implementation dependent

So yes, except the last one. A source

0
source

By extension for typeof statement

IE9 returns

  • "unknown" for SafeArray
  • "data" for VarDate

Both of these are non-standard types defined by host objects.

older IEs are also known for returning "unknowns" for other host objects.

0
source

Source: https://habr.com/ru/post/1392030/


All Articles