为什么 JavaScript 以不同的方式处理字符串和数字之间的加减运算符?

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

Why does JavaScript handle the plus and minus operators between strings and numbers differently?

javascriptstringnumbersoperators

提问by Nirgn

I don't understand why JavaScript works this way.

我不明白为什么 JavaScript 是这样工作的。

console.log("1" + 1);
console.log("1" - 1);

The first line prints 11, and the second prints 0. Why does JavaScript handle the first as a String and the second as a number?

第一行打印 11,第二行打印 0。为什么 JavaScript 将第一行作为字符串处理,第二行作为数字处理?

回答by Bernhard Hofmann

String concatenation is done with +so Javascript will convert the first numeric 1 to a string and concatenate "1" and "1" making "11".

字符串连接是用+Javascript完成的,因此 Javascript 会将第一个数字 1 转换为字符串,并将“1”和“1”连接起来形成“11”。

You cannot perform subtraction on strings, so Javascript converts the second "1" to a number and subtracts 1 from 1, resulting in zero.

您不能对字符串执行减法,因此 Javascript 将第二个“1”转换为数字并从 1 中减去 1,结果为零。

回答by Niet the Dark Absol

+is ambiguous. It can mean "concatenate" or"add". Since one side is a string, it is taken to mean "concatenate", hence the result is 11 (which, by the way, was one of my favourite jokes as a young child. That and "1 + 1 = window", as shown visually: │┼│ ニ ?)

+是模棱两可的。它可以表示“连接”“添加”。由于一侧是一个字符串,所以它表示“连接”,因此结果是 11(顺便说一下,这是我小时候最喜欢的笑话之一。那个和“1 + 1 = 窗口”,如视觉上示出:│┼│ ニ ?

-however has only one meaning: subtract. So it subtracts.

-然而只有一个意思:减法。所以它减去。

This kind of problem is not present in other languages such as PHP, where "concatenate" is .instead of +, making no ambiguity. Still other languages like MySQL don't even have a concatenation operator, instead using CONCAT(a,b,c...).

此类问题在其他语言中不存在,例如 PHP,其中“连接”.代替了+,因此不会产生歧义。还有其他语言(如 MySQL)甚至没有连接运算符,而是使用CONCAT(a,b,c...).

回答by Yury Tarabanko

Because the specexplicitly tells to do so. Page 75. Note the difference between 11.6.1 steps 5-8 and 11.6.2 steps 5-7.

因为规范明确要求这样做。第 75 页。注意 11.6.1 步骤 5-8 和 11.6.2 步骤 5-7 之间的区别。

11.6.1 - describes how addition operator works

11.6.1 - 描述加法运算符的工作原理

1-4. ...

1-4. ...

5. Let lprim be ToPrimitive(lval).

5. 令 lprim 为 ToPrimitive(lval)。

6. Let rprim be ToPrimitive(rval).

6. 令 rprim 为 ToPrimitive(rval)。

7. If Type(lprim) is String or Type(rprim) is String, then

7. 如果 Type(lprim) 是 String 或者 Type(rprim) 是 String,那么

7a. Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)

7a. 返回作为连接 ToString(lprim) 后跟 ToString(rprim) 的结果的字符串

8. Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim)

8. 返回对 ToNumber(lprim) 和 ToNumber(rprim) 应用加法运算的结果

11.6.2 - describes how subtraction operator works

11.6.2 - 描述减法运算符的工作原理

1-4. ...

1-4. ...

5. Let lnum be ToNumber(lval).

5. 令 lnum 为 ToNumber(lval)。

6. Let rnum be ToNumber(rval).

6. 令 rnum 为 ToNumber(rval)。

7. Return the result of applying the subtraction operation to lnum and rnum

7. 返回对 lnum 和 rnum 应用减法运算的结果

SummaryIn case of addition if any of the operands when converted to primitive value without any hints suddenly becomes a string the second one is converted to a string too. In case of subtraction both operands are converted to a number.

总结在加法的情况下,如果任何操作数在没有任何提示的情况下转换为原始值时突然变成字符串,第二个操作数也会转换为字符串。在减法的情况下,两个操作数都转换为数字。

回答by dayuloli

+is both an addition operatorfor numeric variables, and a concatenation operatorfor strings.

+既是数值变量的加法运算符,又是字符串的串联运算符

Whenever there's a string after a +, Javascript will choose to use the +as a concatenation operator and convert (typed) as many terms as possible around the string so it can concatenate them. That's just the behaviour of Javascript. (If you tried console.log(23 + 2 + "." + 1 + 5 + "02" + 02);, you'll get the result 25.15022. The number 02was typed into the string 2before being concatenated.

每当 a 后面有一个字符串时+,Javascript 将选择将+用作连接运算符并在字符串周围转换(键入)尽可能多的术语,以便将它们连接起来。这只是 Javascript 的行为。(如果你尝试过console.log(23 + 2 + "." + 1 + 5 + "02" + 02);,你会得到结果25.15022。数字在连接之前被02输入到字符串2中。

-can only be a subtraction operator, so when given a string, it will implicitly change the type of the string "1"into a numeric 1; if it didn't do that, there's no way "1" - 1would make sense. If you tried console.log(23 + 2 + 1 + 5 - "02" + 03);you'll get 32 - the string 02gets converted into the number 2. The term after the -must be able to be converted into a number; if you tried console.log(23 - 2 - "." - 1 - 5 - 02 - "02");you'll get NaNreturned.

-只能是减法运算符,所以当给定一个字符串时,它会隐式地将字符串的类型"1"改为数字1;如果它不这样做,就没有任何"1" - 1意义。如果你尝试过,console.log(23 + 2 + 1 + 5 - "02" + 03);你会得到 32 - 字符串02被转换成数字2。后面的项-必须能够转换成数字;如果你尝试过,console.log(23 - 2 - "." - 1 - 5 - 02 - "02");你会得到NaN回报。

More importantly, if you tried console.log(23 + 2 + "." + 1 + 5 - "02" + 03);, it will output 26.15, where everything before -was treated as a string (because it contains a string ".", and then the term after the -is treated as a number.

更重要的是,如果您尝试console.log(23 + 2 + "." + 1 + 5 - "02" + 03);,它将输出26.15,其中之前的所有内容都-被视为字符串(因为它包含一个字符串".",然后将 之后的术语-视为数字。

回答by Salman A

There is no dedicated string concatenation operator in JavaScript**. The addition operator +performs either string concatenation or addition, depending on the type of operands:

JavaScript** 中没有专门的字符串连接运算符。加法运算符+执行字符串连接或加法,具体取决于操作数的类型:

"1" +  1  // "11"
 1  + "1" // "11"
 1  +  1  // 2

There is no opposite of concatenation (I think) and the subtraction operator -only performs subtraction regardless of the type of operands:

连接没有对立面(我认为)并且减法运算符-只执行减法而不管操作数的类型:

"1" -  1  // 0
 1  - "1" // 0
 1  -  1  // 0
"a" -  1  // NaN

** The .operator in PHP and &operator in VB are dedicated string concatenation operators.

** .PHP 中的&运算符和 VB 中的运算符是专用的字符串连接运算符。

回答by Giuseppe Pes

According to the standard EcmaScript 262. The +and -operators behave differently when strings are involved. The first converts every value to a string. The second converts every value to a number.

根据标准 EcmaScript 262。当涉及字符串时,+-运算符的行为不同。第一个将每个值转换为字符串。第二个将每个值转换为数字。

From the standard:

从标准:

If Type(lprim) is String or Type(rprim) is String, then Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)

如果 Type(lprim) 是 String 或 Type(rprim) 是 String,则返回作为连接 ToString(lprim) 后跟 ToString(rprim) 的结果的 String

This rules implies that if in the expression there is a string value, all values involved in the +operation are converted to a string. In JavaScript when the +operator is used with strings, it concatenates them. This is why console.log("5"+1)returns "51". 1is converted to a string and then, "5" + "1" are concatenated together.

这个规则意味着如果表达式中有一个字符串值,则所有涉及+操作的值都被转换为一个字符串。在 JavaScript 中,当+运算符与字符串一起使用时,它会将它们连接起来。这就是console.log("5"+1)返回“51”的原因。1转换为字符串,然后将“5”+“1”连接在一起。

Nevertheless, the above rule doesn't apply for the -operator. When you are using a -all values are converted to numbers according to the Standard (see below). Therefore, in this case, "5"is converted to 5and then 1is subtracted.

然而,上述规则不适用于-运营商。当您使用 a 时,-所有值都根据标准(见下文)转换为数字。因此,在这种情况下,"5"被转换为5然后1被减去。

From the standard:

从标准:

5 Let lnum be ToNumber(lval).

6 Let rnum be ToNumber(rval).

5 令 lnum 为 ToNumber(lval)。

6 令 rnum 为 ToNumber(rval)。



Operator definition from the standard EcmaScript 262.

来自标准 EcmaScript 262 的运算符定义。

Operator +: http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.1Operator + definition

运营商 +: http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.1运算符 + 定义

Operator -: http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.2Operator - definition

运营商 -: http://www.ecma-international.org/ecma-262/5.1/#sec-11.6.2运算符 - 定义

回答by GibboK

Using plus and a string ""you basically return a string because you are performing a concatenation:

使用加号和字符串,""您基本上会返回一个字符串,因为您正在执行连接:

typeof ("" + 1 + 0)  // string
typeof (1 + 0)  // number

When using -instead you convert to a number as string concatenation is possible:

使用时,-您可以转换为数字,因为可以进行字符串连接:

typeof ("" - 1 + 0) // number