| typeof The typeof operator is a unary operator, and is used to return the type of data which is within the variable passed to it. The data types may be one of five different types: • boolean • number • object • string • undefined It should be noted that the object type also includes arrays, but it is not recommended you use this feature, as it provides no further information about the type of array within the value. It should also be noted that because the undefined type was not fully implemented until the JavaScript 1.3 and JScript 3.0 versions of the language, you'll find that many browsers will return a value of null when passing a variable that has not been fully defined. syntax: typeof( variable ) EXAMPLE var variableOne = true; var variableTwo = 50; var variableThree = "This is a Valid String Value"; var variableFour; document.write(The following are the data types in the four variables which have been stated above:") document.write("variableOne = " + typeof(variableOne) ); document.write("variableTwo = " + typeof(variableTwo) ); document.write("variableThree = " + typeof(variableThree) ); document.write("variableFour = " + typeof(variableFour) ); There are four variables declared in the above example, each with a different data type. The typeof operator is declared within the document.write statements to return the type of data which resides in each variable. VariableOne is of the boolean type, variableTwo is of the number type, variableThree is of the string type, and variableFour is of the undefined type. For the sake of simpleness, the object type of data returned by the typeof operator is not present in this example. |