This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

typeof

Summary

Returns a string that identifies the data type of an expression.

Syntax

typeof [ ( ] expression [ ) ] ;

Examples

The following example tests the data type of variables.

var index = 5;
 var result = (typeof index === 'number');
 // Output: true

 var description = "abc";
 var result = (typeof description === 'string');
 // Output: true

The following example tests for a data type of undefined for declared and undeclared variables.

var declared;
 var result = (declared === undefined);
 // Output: true

 var result = (typeof declared === 'undefined');
 // Output: true

 var result = (typeof notDeclared === 'undefined')
 // Output: true

 var obj = {};
 var result = (typeof obj.propNotDeclared === 'undefined');
 // Output: true

 // An undeclared variable cannot be compared to undefined,
 // so the next line generates an error.
 //  var result = (notDeclared === undefined);

Remarks

The expression argument is any expression for which type information is sought.

The typeof operator returns type information as a string. There are six possible values that typeof returns: “number,” “string,” “boolean,” “object,” “function,” and “undefined.”

The parentheses are optional in the typeof syntax.

See also

Other articles

Attributions

  • Microsoft Developer Network: Article