本文介绍了将两个变量相加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试将两个整数变量加在一起,但是,我似乎无法弄清楚,因为它只是将它们作为字符串连接?
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);
结果:1010,想要的结果:20
Result: 1010,Want Result: 20
推荐答案
var age_child = parseInt(10);
var age_gap = parseInt(10);
alert(age_child+age_gap); // should now alert 20
澄清一下,在这个确切的例子中,不需要执行 parseInt
.但是,我假设您的代码中也没有 10
,而是变量.
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.
这篇关于将两个变量相加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!