Use parseInt
to convert a string to a number:
var a = '2'; var b = '3'; var sum = parseInt(a,10) + parseInt(b,10); console.log(sum);
Keep in mind that parseInt(str, rad)
only works if str
actually contains several base rad
, so if you want to allow other bases, you will need to manually check them. Also note that you need to use parseFloat
if you want more integers.
source share