typeof Operator (JavaScript)

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

Syntax


typeof[(]expression[)] ;  

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.

Example

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  

Example

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 used in a comparison without  
// the typeof operator, so the next line generates an error.  
//  var result = (notDeclared === undefined);  

Requirements

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

See Also

Array.isArray Function
Object.getPrototypeOf Function
undefined Constant
Comparison Operators
Data Types
Operator Precedence
Operator Summary (JavaScript)