Javascript 将两个变量相加?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7658176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:05:11 来源:igfitidea点击:
Adding two Variables together?
提问by ritch
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
结果:1010,想要的结果:20
回答by Rene Pot
var age_child = parseInt(10);
var age_gap = parseInt(10);
alert(age_child+age_gap); // should now alert 20
回答by sushil bharwani
useparseInt(age_child) + parseInt(age_gap);
用parseInt(age_child) + parseInt(age_gap);