I came across the following table which states the internal [[Class]]
property of an object and its corresponding value that the typeof
operator returns.
Value Class Type
-------------------------------------
"foo" String string
new String("foo") String object
1.2 Number number
new Number(1.2) Number object
true Boolean boolean
new Boolean(true) Boolean object
new Date() Date object
new Error() Error object
[1,2,3] Array object
new Array(1, 2, 3) Array object
new Function("") Function function
/abc/g RegExp object (function in Nitro/V8)
new RegExp("meow") RegExp object (function in Nitro/V8)
{} Object object
new Object() Object object
One thing to note here is the typeof
correctly returns the primitive data types associated in Javascript.
However, it returns an object
type for an array which inherits from the Array.prototype
, but returns a function
type for a function that is inheriting from the Function.prototype
.
Given everything is an object in Javascript (including arrays, functions & primitive data type objects), I find this behaviour of the typeof
operator very inconsistent.
Can someone throw some light on how the typeof
operator works in reality?