This operator produces a string that contains the operand's type.
The typeof operator inspects the operand and returns a string representing its type. The operand is not evaluated. There are times when this is advantageous and can avoid a run-time error.
The string value returned depends on the operand type being evaluated. The typeof operator returns these values:
Type | Result |
---|---|
Undefined | "undefined" |
Infinity | "number" |
NaN | "number" |
Null | "object" |
Boolean primitive | "boolean" |
Number primitive | "number" |
String primitive | "string" |
Boolean() constructor | "boolean" |
Date() constructor | "string" |
Number() constructor | "number" |
RegExp() constructor | "undefined" |
String() constructor | "string" |
Boolean object instance | "object" |
Date object instance | "object" |
Math object instance | "object" |
Number object instance | "object" |
RegExp object instance | "object" |
String object instance | "object" |
Generic object instance | "object" |
Object not supporting a call interface | "object" |
Object that supports a call interface | "function" |
Other host objects | Implementation defined |
typeof any value | "string" |
Note that the values returned are lower-case and are not an exact match for the class of the operand. String objects and primitive strings are both described as having a typeof "string" for instance.
In some documentation the typeof operator is referred to as a function. Since it has optional parentheses and the operand is passed as an argument, it behaves as if it were a function.
The typeof operator is one of few ways in which you can use the contents of a variable that is not yet defined. It will yield the undefined type for variables that have not yet had a value assigned to them. And thus you can determine if it is safe to use them as an RValue in an assignment expression.
The associativity is right to left.
Refer to the operator precedence topic for details of execution order.
In JavaScript version 1.1, you can determine the difference between the undefined and null values.
In JavaScript version 1.3, the === operator will detect the difference.
You cannot distinguish between different kinds of objects. To do that you can compare the object constructor property with one of the built-in types. You can ask the constructor property (if there is one) for its name. Sometimes a toString() conversion on the object will yield a function name and occasionally you may need to check prototype values or test for the existence of properties. If all else fails, you have to assume that it's just an object of arbitrary type.
The typeof operator is not available in Netscape version 2.02.
In Microsoft environments, this is available most of the time, but does not work for certain objects. In particular, there may some objects in WSH for which it is not supported.
// Testing a string value var aString = 'String text'; document.write(typeof(aString)); // Yields "string" // Testing variables that exist but are not yet assigned var aVar1; document.write(typeof aVar1); // Yields "undefined" // Testing variables that do not yet exist document.write(typeof aVar2 ); // Yields "undefined"
ECMA 262 edition 2 - section - 11.1.4
ECMA 262 edition 2 - section - 11.4.3
ECMA 262 edition 3 - section - 11.4.3
O'Reilly JavaScript Definitive Guide - page - 47
Wrox Instant JavaScript - page - 21
Wrox Instant JavaScript - page - 22
Prev | Home | Next |
TypeError object | Up | U |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |