Concatenation prevention

I have been writing JavaScript for 13 years, but I kind of rediscovered it over the past few months as a way to write programs that can be used by anyone who visits a web page without installing anything. For example, http://ab1jx.webs.com/calcs/calcs.html .

I recently discovered that since JavaScript was freely printed by design, it continues to concatenate strings when I want them to add numbers. And this is unpredictable. One routine worked fine for several days, and then when I fed different data into it, the problem succeeded, and I ended up with an incredibly large number.

Sometimes I was lucky if it interfered with ( ) for one term, sometimes I had to resort to parseInt() or parseFloat() for one term. This reminds me a bit of trying to force a float result in C by putting a.00 on one (constant) term. I just did this when I tried += something from an array that I was already loading, doing parseFloat() for everything.

Is this just happening? If I use parseInt() or parseFloat() for at least one of the terms every time I add, will this prevent this? I use Firefox 6 under Linux for recording, but portability between browsers is also a concern.

+4
source share
4 answers

I usually do this:

 var x = 2; var t = "12"; var q = t+x; // q == "122" var w = t*1+x; // *1 forces conversion to number w == 14 

If t is not a number, you will get NaN .

If you multiply by 1 variable, you do not know what type they are. They will be converted to a number. I find this method better than doing int and float casts, because * 1 works with every type of numbers.

The problem you are facing is that functions that retrieve values ​​from the DOM usually return strings. And even if it is a number, it will be presented as a string when it is received.

+7
source

The specification specifies the add operator :

If Type (lprim) is a string or type (rprim) is String, then Return the string that is the result of the union ToString (lprim) and then ToString (rprim)

This means that if at least one statement is a string, the string will be concatenated.

If I use parseInt() or parseFloat() for at least one of the terms every time I add, will this prevent this?

No, all operands must be numbers.

You can easily convert any number string to a number using the unary plus operator (it will not change the value if you are already dealing with a number):

 var c = +a + +b; 
+7
source

You can use the + operator to convert a string to a number.

 var x = '111' +x === 111 
+3
source

Be sure that this is very predictable, you just need to be familiar with the operators and data types of your input.

In short, the evaluation is from left to right, and concatenation will occur whenever a string is present, regardless of which side of the operation.

So for example:

 9 + 9 // 18 9 + '9' // '99' '9' + 9 // '99' + '9' + 9 // 18 - unary plus - '9' + 9 // 0 - unary minus 

Some ternary expressions:

 9 + '9' + 9 // '999' 9 + 9 + '9' // '189' 
+1
source

All Articles