views:

69

answers:

6

Hi I have two variables, 'a' and 'b' in my javascript, and i want to add them together - i assume this code:

var a = 10;
var b = 30
var varible = a + b;

This, puts the two numbers next to eachother... any ideas why... the result should be 40?

Thanks!!

+2  A: 

new is a reserved word, I'd use something else in any case.

And with a normal variable name of c it worked for me:

var a = 10;
var b = 30
var c = a + b;
alert(c);

did the expected and alerted 40

Victor
The code he posted is obviously not what he uses in real life, as (most?) JavaScript interpreters will throw a syntax error in this case. Also, one of his variables probably contains a string.
Harry Vangberg
But that is the error in the posted code. I cannot read unposted code yet, sorry.
Victor
+4  A: 

You probably have strings instead of integers. That is your code really is like this:

var a = "10";
var b = "30";
var c = a + b; // "1030"

There are several ways to convert the strings to integers:

a = parseInt(a, 10);  // Parse the string
b = b * 1;            // Force interpretation as number
John Kugelman
Careful using parseInt without specifying the radix argument.
Joel Coehoorn
A: 

One or both is a string. If you get the values from a HTML input or something, they definitely are. Make sure they're both integers by using parseInt:

var newValue = parseInt(a,10) + parseInt(b,10);

Also, 'new' is a keyword. You can't use that for a variable name :)

Harry Vangberg
careful using parseInt without the radix argument. You can easily get into trouble, especially if there can be leading zeros.
Joel Coehoorn
+1  A: 

new is a keyword in JavaScript. you should not use it to declare your variables or functions. change the variable name from new to something else

Xinus
+1  A: 

Are you sure you didn't do this:

var a = '30';
var b = '40';

Here, I show '30' as a string rather than a number, and I would expect the "+" operator to concatenate two strings. Since this is contrived code, you may not be entirely sure where your variables were initially assign or what type they have. You can check it like this:

var a = '30';
var b = '40';
alert( typeof(a) + '\n' + typeof(b) );

If either of those say 'object' or 'string' rather than 'number' this is your problem. One way this might happen that you didn't expect is with an input. Say you have code like this:

<input id="a" value="30" />
<input id="b" value="40" />
<script language="javascript">
    var a = document.getElementById('a').value;
    var b = document.getElementById('b').value;
</script>

Here, the value of a text input is always a string initially.

If you want to convert a variable to a number first you should use something like myVar - 0 to coerce a numeric operation or the more-formal parseInt() or parseFloat() functions (don't forget the radix parameter for parseInt()). And always check isNaN() on the results.

Joel Coehoorn
+1 why downvoted?
klez
Lack of solution.
Harry Vangberg
He didn't ask for a solution. He asked for the "why". Often that's much more important. You may already know things you can do to fix it, but you want to know why it's broken so you can understand the problem and avoid it altogether in the future. That said, I have added solution suggestions to the bottom of the post.
Joel Coehoorn
+1  A: 

I'm really surprised that noone has until now suggested the obvious: "Casting" with JavaScript (I set it in quotes, because it is no real casting).

var a = "1"; // string
var b = Number(a); // number
var c = String (b); // string again

a + b; // "11"
b + a; // 2
a + c; // "11"

Now, why is this no real casting? Because you don't create a new variable of type "number" but a new object "Number" and initialize it with something that could be numerical.

Boldewyn