May 2008
Intermediate to advanced
172 pages
4h 54m
English
The typeof operator returns a string that
identifies the type of its operand. So:
typeof 98.6
produces 'number'. Unfortunately:
typeof null
returns 'object' instead of 'null'. Oops. A better test for null is simply:
my_value === null
A bigger problem is testing a value for objectness. typeof cannot distinguish between null and objects, but you can because null is falsy and all objects are truthy:
if (my_value && typeof my_value === 'object') {
// my_value is an object or an array!
}Also see the later sections "NaN" and "Phony Arrays."
Implementations disagree on the type of regular expression objects. Some implementations report that:
typeof /a/
is 'object', and others say that it is 'function'. It might have been more useful to report
'regexp', but the standard does not allow
that.