Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding two Variables together?

Trying to add two integer variables together, however, I can't seem to figure it out as it just joins them as strings?

var age_child = 10;
var age_gap = 10
alert(age_child+age_gap);

Result: 1010, Want Result: 20

like image 787
ritch Avatar asked Oct 05 '11 07:10

ritch


2 Answers

var age_child = parseInt(10);
var age_gap = parseInt(10);

alert(age_child+age_gap); // should now alert 20

To clarify, in this exact example it is not required to do parseInt. However, I assumed you didn't exactly have 10 in your code either and they're instead variables.

like image 104
Rene Pot Avatar answered Sep 19 '22 08:09

Rene Pot


use parseInt(age_child) + parseInt(age_gap);

like image 38
sushil bharwani Avatar answered Sep 20 '22 08:09

sushil bharwani