Jump to content

"weak" Variable Type


Don59

Recommended Posts

Presumably, Javascript automatically chooses the right variable type, based on what values are put into the variable. Sounds good, but I came across a bug in my Javascript, producing some wacky results, from some very simple math. After hours of hair-pulling agony, I finally figured out that it was combining two numbers as if they were both strings, rather than adding the numbers together. For example:X = 5Y = 6Z = X + YAnd the result, was that Z equaled "56" (Note, the program was much more complex than what's written here, and the variables had already been in use).I know that variables are defined as they are used. Can their "type" change, as the script continues? I.e., can it be numeric initially, and then become a string later on, as text is stored in it? And then back to a number later on, as only numeric values are put into it?And are there any standard-practice methods of making sure that the numeric value of variables are used, rather than string?Thanks,Don

Link to comment
Share on other sites

Casper,Thanks, and I will use that from now on, every time I need to make sure it's numeric. But there must be an easier way -- some way to be confident that variables holding numbers will be numeric variables and not strings, short of passing every variable through a function, every time it's used.Don

Link to comment
Share on other sites

Any value that comes out of a text input, textarea, prompt, or innerHTML is a string and must be cast to a number, implicitly or explicitly, if you want it to behave like a number.The + symbol is the big difficulty, since it is used to add numbers and also to concatenate strings. So there is ambiguity, and you get goofy results, as you noted. The other math operators will cast a string to a number if they can. So:var str = "5";var result = str * 5;var tp = typeof result;// result equals 25// tp equals "number"BUT:result = str * "2W";// result = NaN

Link to comment
Share on other sites

I usually use parseInt to convert to an int, and you check if it succeeded using isNaN.

var x = "5";x = parseInt(x, 10);if (isNaN(x)){  alert("x is not a number");}

Since math operators will automatically convert to numbers if necessary, you can also make sure that your math operations are math instead of string operations. If you add a 0 it will have the desired effect.z = 0 + x + y;

Link to comment
Share on other sites

Any value that comes out of a text input, textarea, prompt, or innerHTML is a string and must be cast to a number, implicitly or explicitly, if you want it to behave like a number.
That's probably what happened here: Got the number out of a text input box, and just presumed it would be handled as a number. Wrong answer :)
The + symbol is the big difficulty, since it is used to add numbers and also to concatenate strings. So there is ambiguity, and you get goofy results, as you noted. The other math operators will cast a string to a number if they can. So:var str = "5";var result = str * 5;var tp = typeof result;// result equals 25// tp equals "number"BUT:result = str * "2W";// result = NaN
Interesting. But if you had written for the second line, "var result = str + str" then it would have returned "55" probably, right?That is a tricky but very important issue.Thanks all for the ideas.Don
Link to comment
Share on other sites

Since math operators will automatically convert to numbers if necessary, you can also make sure that your math operations are math instead of string operations. If you add a 0 it will have the desired effect.z = 0 + x + y;
Whoa. Clever trick. I like it. Thanks.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...