@Jeff_MottSep 12.2005 — #var1 = parseFloat(var1)||0; // for floats
var2 = parseInt(var2)||0; // for integers[/quote]Or the [font=courier new]Number()[/font] function, which handles numbers in any known format, and is more intelligent about what it returns.
@Jeff_MottSep 12.2005 — #I would say parseFloat is more intelligent as it will turn "100cm" into 100.[/quote]Despite that side effect parseFloat is still completely ignorant to what the suffix that it leaves off means. For instance, parseFloat("100cm") + parseFloat("100mm") will not give you the answer you're looking for. And parseFloat("100 blah blah blah") is certainly not going to be the number of units of measure you're looking for. It's not intelligent, it just happens to be a convienence in very special cases when you know what the input is (note that knowing what input [b]is[/b] is not the same as knowing what input [b]should be[/b]).
Another example, what if I asked you personally to give me the sum of "1Q$0!" and "5L^93~". Would you tell me the answer is 6? Or would you say to me: these aren't numbers; I can't add them? parseFloat would tell you the answer is 6, whereas Number would tell you these are not numbers.
Using the Number function ensures that your input is indeed a number and not some goop that we pick digits out of.
This is only valid in Javascript 2.0 and no browser supports that yet. It may also valid is JScript when running as part of ASP on the server (but I am not sure about that).
Javascript 1.5 doesn't allow you to define the type of variables, it is derived from whatever is assigned to it.