Reputation: 917
I need to sum several values in javascript. I've tried by using following code
var a = 2;
var b = 5;
c = a+b;
But, instead of calculating the values of a and b, the output (c) only combine those two values. So the output given is :
c = 25
I believe you guys can help me easily about this. Thx before. Regard Andha.
Upvotes: 11
Views: 115274
Reputation: 127
-Is important to apply Number() to every value. The ideal way is:
var sum = 0
sum = Number('93') + Number('7') //result 100
-instead of this way (careful with this)
var sum = 0
sum = Number('97' + '3') //result 937
-and careful with this (as variable is going to assign string type by default)
var sum = 0
sum = Number('97') + '3' //result "973"
Upvotes: 2
Reputation: 61
You can simply convert string
to a number
by adding +
before it. For somebody can be more readable.
Example:
const a = "2";
const b = "5";
const c = +a + +b
or const c = (+a) + (+b)
may be more readable.
That will first convert the string to a Number.
Upvotes: 0
Reputation: 21
Use parseInt()
:
var a=2;
var b=5;
c=parseInt(a)+parseInt(b);
Upvotes: 2
Reputation: 89
The author has probably put "simplified" code so we can get an idea. Had same problem, while getting input values. JS interpreted it as string. Using "Number()" solved the problem:
var sum = Number(document.getElementById("b4_f2_"+i).value) + Number(document.getElementById("b4_f3_"+i).value) + Number(document.getElementById("b4_f4_"+i).value);
Upvotes: 6
Reputation: 34072
Your code is adding (concatenating) strings. Are you sure that the code you posted represents your problem? What you have written should work. Be sure in the real code you're not saying:
var a = '2'; // or something similar
Or if the values are parsed from somewhere, be sure to call parseInt(a, 10)
on them before doing the addition, 10 being the radix.
Or as pointed out in the comments the Number
function would probably suit your purposes.
Upvotes: 8
Reputation: 4976
Make sure the values are numbers, otherwise they will concat instead of suming.
a = parseInt(a, 10); // a is now int
Upvotes: 13
Reputation: 19550
The code you show will not work the way you describe. It will result in 7
.
However, when attempting to perform addition, if either or both numeric values are actually numeric strings, the other values will be cast to strings and they will be concatenated.
This is most likely to happen when attempting to read form values, reading cookies, or some other sort of HTTP header. To convert a string to a number, you need to use parseInt()
[docs]. Read through the docs on it and be sure to pay attention to, and provide, the second parameter (radix
) to ensure the casting from string to number uses the base you expect. (The lack of info on radix
in other answers is the primary reason I went ahead and posted an answer even though others had already mentioned parseInt()
.)
Also, FYI, Another handy function to use when dealing with unknown values and hoping to perform mathematic operations is isNaN()
[docs].
Upvotes: 2
Reputation: 56457
This works fine:
var a = 2;
var b = 5;
var c = a + b; // c is now 7
Upvotes: 2