typeof (Operator/unary)

An operator that yields the type of an operand.

Availability:

ECMAScript edition - 2
JavaScript - 1.1
JScript - 1.0
Internet Explorer - 3.02
Netscape - 3.0
Opera - 3.0
Property/method value type:String primitive
JavaScript syntax:-typeof anOperand
-typeof(anOperand)
Argument list:anOperandAn object or variable to check for type.

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:

TypeResult
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 objectsImplementation 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.

Warnings:

Example code:

   // 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

Cross-references:

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