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
typeof
comparison 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
number
data type is created and thetypeof
operator is used to reveal the data type in the console.