11

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.

Andha
  • 907
  • 2
  • 11
  • 22

8 Answers8

13

Make sure the values are numbers, otherwise they will concat instead of suming.

a = parseInt(a, 10); // a is now int 
kjetilh
  • 4,821
  • 2
  • 18
  • 24
8

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.

numbers1311407
  • 33,686
  • 9
  • 90
  • 92
  • yeah. i don't use any quotes for the values. i also already converted them to int by using parseInt(a) and parseInt(b). am i doing it right ? – Andha Aug 13 '11 at 21:56
  • 3
    well it depends. `parseInt(a)` doesn't change the `a` variable, it returns the parsed int. So you might need to `a = parseInt(a)` or `c = parseInt(a) + parseInt(b)` – numbers1311407 Aug 13 '11 at 21:58
  • 1
    Be sure to use the `radix` parameter in calls to `parseInt()`. – JAAulde Aug 13 '11 at 22:12
  • I can see that specifying the radix would help for readability, but is it really necessary here? It's probably a more than valid assumption that this app is going to be summing base 10 numbers. Edit: I guess given that he is parsing numbers there's the chance a '0' could be prepended or something. I concede the point :-P – numbers1311407 Aug 13 '11 at 22:16
  • I think it would be crazy to do otherwise, numbers1311407. Here's some fun, parseInt(null, 27); – sciritai Aug 13 '11 at 22:34
  • 1
    Better to convert to number using, well ehr, `Number`. No need for radix - so `Number('08')` returns 8, not 0. – KooiInc Aug 13 '11 at 23:44
6

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);
random
  • 8,568
  • 12
  • 50
  • 85
dMole
  • 89
  • 1
  • 9
2

Use parseInt():

var a=2;
var b=5;
c=parseInt(a)+parseInt(b);
msanford
  • 11,803
  • 11
  • 66
  • 93
2

This works fine:

var a = 2; 
var b = 5; 
var c = a + b; // c is now 7
Jordão
  • 55,340
  • 13
  • 112
  • 144
2

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].

JAAulde
  • 19,250
  • 5
  • 52
  • 63
  • http://www.javascripttoolbox.com/bestpractices/#onclick states that you can do a: X = (+"2") + (+"b5") - which I find interresting :) – MikeyKennethR Aug 14 '11 at 16:05
1

-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"
0

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.