Reputation: 1808
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
Upvotes: 12
Views: 63907
Reputation: 1
let x = 5;
let y = 10;
let sum = x + y;
console.log('The sum of x and y is: ' + sum);
Upvotes: 0
Reputation: 24815
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.
Upvotes: 23