添加不适用于JavaScript [英] Addition is not working in JavaScript
问题描述
我正在尝试学习Javascript。在这里,我对以下代码感到困惑。
I am trying to learn Javascript. Here I am confused with the following code.
http ://rendera.heroku.com/usercode/eae2b0f40cf503b36ee346f5c511b0e29fc82f9e
当我把 x + y
时在功能中它出错了。例如 2 + 2 = 22
, 5 + 7 = 57
When I put x+y
in the function it is going wrong. For example 2+2=22
, 5+7=57
但 /
, *
, -
是工作。为什么 +
无效?请帮我。非常感谢提前
But /
, *
, -
are working. Why is +
not working? Please help me. Thanks a lot in advance
推荐答案
其中一个或两个变量是字符串而不是数字。这使得 +
do字符串连接。
One or both of the variables is a string instead of a number. This makes the +
do string concatenation.
'2' + 2 === '22'; // true
2 + 2 === 4; // true
其他算术运算符 / * -
将对字符串执行 toNumber
转换。
The other arithmetic operators / * -
will perform a toNumber
conversion on the string(s).
'3' * '5' === 15; // true
将字符串转换为数字的快捷方法是使用一元 +
运营商。
A quick way to convert a string to a number is to use the unary +
operator.
+'2' + 2 === 4; // true
...或您的变量:
+x + +y
这篇关于添加不适用于JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!