Skip to content Skip to sidebar Skip to footer

Does The Javascript Typeof Operator Ever Return An Upper Case String?

I recently came across the following line of code: var type = (typeof x).toLowerCase(); Note that in the above code, x will only ever be a string, a number, or undefined. I questi

Solution 1:

Spidermonkey seems to return only these:

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

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

The same 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

No idea about MS, I guess they don't use custom typeof either, but you never know with them.

There are six possible values that typeof returns: "number," "string," "boolean," "object," "function," and "undefined."

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

Solution 2:

return values for the typeof operator

Undefined: "undefined" Null: "object" Boolean: "boolean" Number: "number" String: "string" Object (native and doesn't implement Call): "object" Object (native and implements Call): "function" Object (host): Implementation-dependent

So yes except the last. Source

Solution 3:

As per extensions to typeof operator

IE9 returns

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

Both of which are non-standard types defined by host objects

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

Post a Comment for "Does The Javascript Typeof Operator Ever Return An Upper Case String?"