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:
|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.
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"
|See also:||Associativity, Cast operator, class, Enquiry functions, Equal to (==), Global object, Grouping operator ( ), Identically equal to (===), NOT Equal to (!=), NOT Identically equal to (!==), Object inspector, Operator, Operator Precedence, Reference, Special type, Unary expression, Unary operator, void|
ECMA 262 edition 2 - section - 11.1.4
ECMA 262 edition 2 - section - 11.4.3
ECMA 262 edition 3 - section - 11.4.3