ritch
ritch

Reputation: 1808

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

Upvotes: 12

Views: 63907

Answers (3)

Pranav
Pranav

Reputation: 1

let x = 5;
let y = 10;
let sum = x + y;

console.log('The sum of x and y is: ' + sum);

Upvotes: 0

Rene Pot
Rene Pot

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

sushil bharwani
sushil bharwani

Reputation: 30177

use parseInt(age_child) + parseInt(age_gap);

Upvotes: 8

Related Questions