PDA

View Full Version : 1 + 1 will output 11 and not 2...

Dennis_Gull
07-04-2007, 12:21 AM
Hello, got a problem with addition, I have a function that will need a number. Like:
my_function(numb)

and now I want to get the next number (numb + 1) but It will output as 11 instead of 1 + 1 = 2..
I tried to use this:
var next = numb + 1..

and this:
var next = 0;
next = numb + 1;

and this:
var one = 1;
var next numb + one;

but it will give me the same result all the time, the previous (-1) works great but not the +1..

thetestingsite
07-04-2007, 12:40 AM
Try numb++, usually that works.

Dennis_Gull
07-04-2007, 01:05 AM
thanks it worked.

Twey
07-04-2007, 01:08 AM
The + operator in Javascript is overloaded: it performs two quite distinct functions. It performs arithmetical addition on numbers, but concatenation on strings. The latter always takes precedence over the first, so if a string is involved on either side of the operator, concatenation will occur. That is to say:
1 + 1 == 2;
1 + "1" == "11";
"1" + 1 == "11";If you're not sure whether you're dealing with a number or a string, be safe and convert it. There are several methods of doing this:
new Number(n);
parseFloat(n, 10);
parseInt(n, 10);
n - 0;
-n;
+n;
n * 1;

djr33
07-04-2007, 02:36 AM
Isn't the easiest way, then to do
a + 1*b; ?
(Assuming you know that a is a constant, or that a is declared literally as an integer, say 3 + 1*b; )

Or, you could just subtract it's opposite:

a-(-b);

That's probably the easiest, in fact. This would, since it's a mathematical operation without a string-alternative, convert both a and b, I'd assume, so it's a very efficient way to do all of this at once.

Trinithis
07-04-2007, 05:34 AM
Are the (a+b*1) or (a- -b) shorthands executed faster than the parse functions?

djr33
07-04-2007, 06:05 AM
My guess would be that any simple math function would be faster than any more complex function.

Twey
07-04-2007, 11:21 AM
Yes, +"3" performed an average of 0.0053ms faster than parseInt("3", 10) over 100,000 iterations in SpiderMonkey.

Digger3000
07-04-2007, 11:50 AM
I would just do this:

var next=numb-0+1;

Twey
07-04-2007, 02:27 PM
Personally, I prefer the form:
var next = +numb + 1;However, be aware that parse(Int|Float) don't do quite the same things as the simple operator trickery; they will ignore trailing characters, for a start. +"3j" is NaN, but parseInt("3j") is 3.

mwinter
07-04-2007, 05:12 PM
If you're not sure whether you're dealing with a number or a string, be safe and convert it. There are several methods of doing this:

new Number(n);

No! That evaluates to a Number object, not a number. Whilst it should work, it will involve further conversion (through use of the valueOf method). Instead, call the constructor function as a function:

Number(n)

-n;

Just to point out the obvious: this will evaluate to a number with reversed sign.

Isn't the easiest way, then to do
a + 1*b; ?

There isn't really any objective answer to that, though for compactness I would use unary plus (+) and for readability, the Number constructor function:

a + (+b)
a + Number(b)

The parentheses emphasis the meaning, as well as help prevent an accident like confusing "+ +b" for "++b".

Yes, +"3" performed an average of 0.0053ms faster than parseInt("3", 10) over 100,000 iterations in SpiderMonkey.

Unary plus should be the fastest of all techniques.