typeof Operator (JavaScript)
Returns a string that identifies the data type of an expression.
typeof[(]expression[)] ;
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.
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);
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. Also supported in Windows Store apps. See Version Information.