Your Search Results



    The typeof operator returns a string indicating the type of the unevaluated operand.

    Implemented in: JavaScript 1.1
    ECMA Version: ECMA-262 (and ECMA-357 for E4X objects)


    The typeof operator is followed by its operand:

    typeof operand


    operand is an expression representing the object or primitive whose type is to be returned.


    This table summarizes the possible return values of typeof:

    Type Result
    Undefined "undefined"
    Null "object"
    Boolean "boolean"
    Number "number"
    String "string"
    Host object (provided by the JS environment) Implementation-dependent
    Function object (implements [[Call]] in ECMA-262 terms) "function"
    E4X XML object "xml"
    E4X XMLList object "xml"
    Any other object "object"


    Normal cases

    // Numbers
    typeof 37 === 'number';
    typeof 3.14 === 'number';
    typeof Math.LN2 === 'number';
    typeof Infinity === 'number';
    typeof NaN === 'number'; // Despite being "Not-A-Number"
    typeof Number(1) === 'number'; // but never use this form!
    // Strings
    typeof "" === 'string';
    typeof "bla" === 'string';
    typeof (typeof 1) === 'string'; // typeof always return a string
    typeof String("abc") === 'string'; // but never use this form!
    // Booleans
    typeof true === 'boolean';
    typeof false === 'boolean';
    typeof Boolean(true) === 'boolean'; // but never use this form!
    // Undefined
    typeof undefined === 'undefined';
    typeof blabla === 'undefined'; // an undefined variable
    // Objects
    typeof {a:1} === 'object';
    typeof [1, 2, 4] === 'object'; // use Array.isArray or to differentiate regular objects from arrays
    typeof new Date() === 'object';
    typeof new Boolean(true) === 'object'; // this is confusing. Don't use!
    typeof new Number(1) === 'object'; // this is confusing. Don't use!
    typeof new String("abc") === 'object';  // this is confusing. Don't use!
    // Functions
    typeof function(){} === 'function';
    typeof Math.sin === 'function';


    typeof null === 'object'; // This stands since the beginning of JavaScript

    In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the bogus typeof return value. (reference)

    A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in typeof null === 'null'.

    Regular expressions

    Callable regular expressions were a non-standard addition in some browsers (need reference to say which).

    typeof /s/ === 'function'; // Chrome 1-12 ... // Non-conform to ECMAScript 5.1
    typeof /s/ === 'object'; // Firefox 5+ ...    // Conform to ECMAScript 5.1

    Other quirks

    alert in old Internet Explorer versions

    On IE 6, 7, and 8, typeof alert === 'object'

    Note: Not a quirk. It's telling the truth. Lots of old IE host objects are objects not functions


    ECMA-262 section 11.4.3

    See also

    Document Tags and Contributors

    Contributors to this page: Sevenspade
    Last updated by: Sevenspade,