Is This Variable Number or String

RAMP-TS

Is This Variable Number or String?

Sometimes you have a variable in Javascript and do not know whether it is a number or a string. You can test the type of a variable by using the typeof() operator like this:

Var x     = 1.234;
Var y     = "Hello";
Var Type1 = typeof(x);
Var Type2 = typeof(y); 

Alert(Type1 + " and " + Type2);

This code displays the message "number and string".

There are six possible values that typeof returns: "number," "string," "boolean," "object," "function," and "undefined." The most useful are "number", "string" and "undefined".

"undefined" is useful because it tells you that something does not exist yet (ie: it's undefined) so sometimes you see code like this:

if (typeof(objGlobal.CustomerNumber) = "undefined")) objGlobal.CustomerNumber = "12345";