Undefined Data Type

The Undefined type only has the value - undefined. A variable is only assigned the value undefined when a variable is created but not initialized. A variable can also be initialised with the value undefined, however, this is not required and is not a good practice.


    var result; // not initialised
    alert(typeof result); // undefined


    var result2 = undefined;
    alert(typeof result2); // undefined

    alert(typeof result3); // undefined - result3 variable does not exist

The Undefined type cannot be used for any valid operations except for calling the typeof operator. As shown in the examples above, calling typeof on an uninitialised variable will return "undefined".

The typeof operator can also be called for variables that has been not been declared. JavaScript returns "undefined" for when calling typeof on undeclared variables which could be confusing as the variable has not been declared.


    alert(typeof result3); // undefined - result3 variable not declared

Note: it is recommended to always initialise a variable to avoid any failures when using the variable.