Scripter Javascript Tutorial
javascriptmusiclogicscripterbookexcerpttutorial34 typeof
comparison
MDN: Operators: typeof
typeof returns true if a variable’s value is a
particular data type and uses strings for its returned values.
var str = "foobar";
if ( typeof str == "string" ) {
Trace(str); // "string"
}
var num = 60;
Trace(typeof num); // "number"In the above example:
Line 1: A variable is declared and assigned a string value.
Line 2: The
typeofcomparison is used to determine whether or not the value is of type “string”.Line 3: If the data type is
string, then the script does something with that variable.Line 5-6: A variable of the
numberdata type is created and thetypeofoperator is used to reveal the data type in the console.