加法在 JavaScript 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8377410/
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 05:45:10  来源:igfitidea点击:

Addition is not working in JavaScript

javascriptaddition

提问by Theepan K.

I am trying to learn Javascript. Here I am confused with the following code.

我正在尝试学习 Javascript。在这里,我对以下代码感到困惑。

http://rendera.heroku.com/usercode/eae2b0f40cf503b36ee346f5c511b0e29fc82f9e

http://rendera.heroku.com/usercode/eae2b0f40cf503b36ee346f5c511b0e29fc82f9e

When I put x+yin the function it is going wrong. For example 2+2=22, 5+7=57

当我x+y输入函数时,它出错了。例如2+2=225+7=57

But /, *, -are working. Why is +not working? Please help me. Thanks a lot in advance

但是/*-正在工作。为什么+不工作?请帮我。非常感谢提前

回答by RightSaidFred

One or both of the variables is a string instead of a number. This makes the +do string concatenation.

一个或两个变量是字符串而不是数字。这使得+do 字符串连接。

'2' + 2 === '22';  // true

2 + 2 === 4;  // true

The other arithmetic operators / * -will perform a toNumberconversion on the string(s).

其他算术运算符/ * -将对toNumber字符串执行转换。

'3' * '5' === 15;  // true

A quick way to convert a string to a number is to use the unary +operator.

将字符串转换为数字的一种快速方法是使用一元运算+符。

+'2' + 2 === 4;  // true

...or with your variables:

...或者你的变量:

+x + +y

回答by James Montagne

+has two uses. One is addition, the other however is string concatenation. If one or both of your variables is a string, then +will concatenate them.

+有两个用途。一种是加法,另一种是字符串连接。如果您的一个或两个变量是字符串,+则将它们连接起来。

You will need to use parseIntor parseFloatto turn a string into a number.

您将需要使用parseIntparseFloat将字符串转换为数字。

回答by danwellman

In Javascript the + operator can either perform addition or concatenation depending on the type of its operands. When numbers are used with + it uses addition, but when strings are used with + it concatenates (joins the strings) instead

在 Javascript 中, + 运算符可以根据其操作数的类型执行加法或连接。当数字与 + 一起使用时,它使用加法,但当字符串与 + 一起使用时,它会连接(连接字符串)

回答by Siddhu

If the numbers that you are trying to add are 10 and 12, if they resulting sum is supposed to be 22, then you should probably do it as

如果您尝试添加的数字是 10 和 12,如果它们的总和应该是 22,那么您可能应该这样做

+10 + +12

And the result might be a string like 1012 if one or both of the numbers is a string.

如果其中一个或两个数字是字符串,则结果可能是 1012 之类的字符串。

回答by chrisrth

this works every time

这每次都有效

((x*1) + (y*1))

回答by Dmitri Pavlutin

The addition operator works the following way:
1) If at least one operand is a string, then another is converted to string and concatenation is performed;

加法运算符的工作方式如下:
1) 如果至少一个操作数是字符串,则将另一个操作数转换为字符串并执行连接;

1 + "2"        // "12"
"2" + "3"      // "23"
"2" + null     // "2null", null is converted to "null"

2) In other cases both operands are converted to numbers:

2) 在其他情况下,两个操作数都转换为数字:

1 + null      // 2, null is converted to 0
1 + undefined // NaN, undefined is converted to NaN

Check the post JavaScript's addition operator demystifiedfor more details.

查看帖子JavaScript 的加法运算符揭秘以获取更多详细信息。

回答by aravind3

Unary plus should work:

一元加应该工作:

var totalVal = (+2) + (+2);

alert(totalVal);
// result 4