You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Show Contents List

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(error); Var Type2 = typeof(thumbs up);  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";
Show Contents List

  • No labels