javascript 为什么 1 + '1' = '11' 但 1*'1' = 1

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

Why is 1 + '1' = '11' but 1*'1' = 1

javascript

提问by Pawan

In case of operation

操作时

1 + '1',

1 + '1',

the number 1 is converted to string and appended to the later string then why isn't it the case for

数字 1 被转换为字符串并附加到后面的字符串然后为什么不是这样

1 * '1'

1 * '1'

回答by Matthew

Because +is overloaded.

因为+超载了。

+can mean either additionor string concatenation. In the former case, JavaScript attempts to do string concatenation rather than addition, so it converts everything to a string and carries out the string concatenation. In the latter case, the only option is to multiply, so it converts everything to something that can be multiplied and carries out the multiplication.

+可以表示加法字符串连接。在前一种情况下,JavaScript 尝试进行字符串连接而不是加法,因此它将所有内容转换为字符串并执行字符串连接。在后一种情况下,唯一的选择是乘法,因此它将所有东西都转换为可以乘法的东西并执行乘法。

dfsqlinked the specification of addition syntaxin the comments under your question, which explains why JS attempts string concatenation instead of addition: It checks whether the things you're adding together are strings, and then if at least one of them is, attempts string concatenation - and otherwise, attempts addition.

dfsq在您的问题下的评论中链接了加法语法规范,这解释了为什么 JS 尝试字符串连接而不是加法:它检查您加在一起的东西是否是字符串,然后如果其中至少一个是字符串,则尝试字符串串联 - 否则,尝试添加。

回答by neelsg

The +is a concatenation operator for strings. As a result, the number gets converted to a string and then concatenated. The concatenation takes preference over numeric addition. If you want to make it add them instead, use parseInt, like 1 + parseInt('1')

+是字符串连接运算符。结果,数字被转换为字符串,然后连接起来。串联优先于数字加法。如果您想让它添加它们,请使用parseInt,例如1 + parseInt('1')

The *is not a valid operator for strings at all, so it converts the string to a number and then does the operation.

*不是在所有字符串有效的运营商,所以它的字符串转换成数字,然后做操作。

This is a simple case, so the order of operands don't matter. If you get more complex, it tends to get even more interesting. For instance:

这是一个简单的情况,因此操作数的顺序无关紧要。如果你变得更复杂,它往往会变得更有趣。例如:

1 + 1 + '1' = '21'
'1' + 1 + 1 = '111'

For more information, check out this MDN article on the matter

有关更多信息,请查看有关此问题的这篇 MDN 文章

回答by sumit

+is used for string concatenation *is used for multiplicatio

+用于字符串连接 *用于乘法

In 1 + '1' '+' will concatenate 1 with '1'

在 1 + '1' '+' 将连接 1 和 '1'

You need to do following

你需要做以下

1 + parseInt('1')

回答by selvakumar gopal

"+" Operator is used for both string concatenation and normal mathematical addition so when we use this operator between a number and string it will just concatenate those two. But "*" Operator not like that it will only perform multiplication if this used between a number and a pure string it wont give proper output.But, if it is used between a number and again a number in string format it will consider both as number and give the multiplication of those two.

“+”运算符用于字符串连接和普通数学加法,因此当我们在数字和字符串之间使用此运算符时,它只会连接这两者。但是“*”运算符不像那样,它只会在数字和纯字符串之间使用它时才执行乘法,它不会给出正确的输出。但是,如果它在数字和字符串格式的数字之间使用,它会认为两者数并给出这两者的乘法。

回答by Snehal Nagdeote

In javascript + indicates concatination. That's why when you try to add a number(i.e. 1) to a string ('1'),it becomes 11. And it treats * as multipication, so it multiplies a number (1) with a string ('1') and gives result as 1. e.g. (1*a= a).

在 javascript 中 + 表示串联。这就是为什么当您尝试将数字(即 1)添加到字符串 ('1') 时,它变为 11。并且它将 * 视为乘法,因此它将数字 (1) 与字符串 ('1') 相乘,然后结果为 1。例如 (1*a= a)。