string JavaScript 将字符串添加到数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16522648/
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
JavaScript adding a string to a number
提问by SineLaboreNihil
I was reading the re-introduction to JavaScript on MDNand in the section Numbersit said that you can convert a string to a number simply by adding a plus operator in front of it.
我正在阅读MDN 上对 JavaScript的重新介绍,在Numbers部分,它说您可以通过在字符串前面添加加号运算符将字符串转换为数字。
For example:
例如:
+"42" which would yield the number output of 42.
+"42" 这将产生 42 的数字输出。
But further along in the section about Operatorsit says that by adding a string "something" to any number you can convert that number to a string. They also provide the following example which confused me:
但是在关于运算符的部分进一步说明,通过向任何数字添加字符串“某物”,您可以将该数字转换为字符串。他们还提供了以下让我感到困惑的示例:
"3" + 4 + 5 would presumably yield a string of 345 in the output, because numbers 4 and 5 would also be converted to strings.
"3" + 4 + 5 大概会在输出中产生一个 345 的字符串,因为数字 4 和 5 也会被转换为字符串。
However, wouldn't 3 + 4 + "5" yield a number of 12 instead of a string 75 as was stated in their example?
但是, 3 + 4 + "5" 不会产生数字 12 而不是他们的示例中所述的字符串 75 吗?
In this second example in the section about operators wouldn't the + operator which is standing in front of a string "5" convert that string into number 5 and then add everything up to equal 12?
在关于运算符的部分的第二个示例中,位于字符串“5”前面的 + 运算符不会将该字符串转换为数字 5,然后将所有内容相加等于 12?
回答by epascarello
What you are talking about is a unary plus. It is different than the plus that is used with string concatenation or addition.
你所说的是一元加号。它与用于字符串连接或加法的加号不同。
If you want to use a unary plus to convert and have it added to the previous value, you need to double up on it.
如果要使用一元加号进行转换并将其添加到先前的值,则需要将其加倍。
> 3 + 4 + "5"
"75"
> 3 + 4 + +"5"
12
Edit:
编辑:
You need to learn about order of operations:
您需要了解操作顺序:
+
and -
have the same precedence and are associated to the left:
+
并-
具有相同的优先级并与左侧相关联:
> 4 - 3 + 5
(4 - 3) + 5
1 + 5
6
+
associating to the left again:
+
再次关联到左侧:
> 3 + 4 + "5"
(3 + 4) + "5"
7 + "5"
75
unary operators normally have stronger precedence than binary operators:
一元运算符通常比二元运算符具有更高的优先级:
> 3 + 4 + +"5"
(3 + 4) + (+"5")
7 + (+"5")
7 + 5
12
回答by Anickyan
You could also use parseInt() or parseFloat(), like this:
您还可以使用 parseInt() 或 parseFloat(),如下所示:
> 1 + 2 + "3"
"33"
> 1 + 2 + parseInt(3)
6
I think that's alot cleaner than using +"3", but that is just my opinion.
我认为这比使用 +"3" 更干净,但这只是我的意见。
回答by Agus
The answer can be found in Ecma262.pdf section 11.6.1:
答案可以在 Ecma262.pdf 第 11.6.1 节中找到:
If Type(lprim) is String or Type(rprim) is String, then a. Return the String that is the result of concatenating ToString( lprim) followed by ToString(rprim).
如果 Type(lprim) 是 String 或 Type(rprim) 是 String,则 a. 返回连接 ToString(lprim) 后跟 ToString(rprim) 的结果的字符串。
So that will resolve all operations according to precedence, so that as soon the string is found any number, the number is converted to string.
这样将根据优先级解决所有操作,以便一旦找到任何数字字符串,该数字就会转换为字符串。
4 + 3 + "5"
"75"
4 + 3 + "5" + 3
"753"
To read the whole standard, go here.
要阅读整个标准,请转到此处。
回答by Moritz Roessler
When you look at Step 7 and "Note 2" at The Addition operator ( + )(§11.6.1) in the ES5 specs,
当您查看ES5 规范中加法运算符 ( + )(§11.6.1) 中的第 7 步和“注释 2”时 ,
it says
它说
If Type(lprim) is String` orType(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
NOTE 2 Step 7 differs from step 3 of the comparison algorithm for the relational operators (11.8.5), by using the logical-or operation instead of the logical-and operation.
注 2:步骤 7 与关系运算符(11.8.5)的比较算法的步骤 3 不同,使用逻辑或运算代替逻辑与运算。
Meaning if either 7
(3+4
) or"5"
(||
not &&
) is typeof "string"
toString()
is applied to both operands.
意思是将7
( 3+4
)或"5"
( ||
not&&
)typeof "string"
toString()
应用于两个操作数。
So the addition is actually applied to
所以加法实际上应用于
"7"
and "5"
-> "7" + "5" //"75"
"7"
和"5"
->"7" + "5" //"75"
回答by Sanjeev Singh
A smple + operator in javascript is used for concatenation and not to add.
javascript 中的 smple + 运算符用于连接而不是添加。
A bracket and a + operator before the string format integer variable will do the job every time.
字符串格式整数变量之前的括号和 + 运算符每次都会完成这项工作。
This always works fine.
这总是工作正常。
1 + (+"2") = 3
Because placing + before string converts the string variable into number.
因为在字符串之前放置 + 会将字符串变量转换为数字。
fiddle here: http://jsfiddle.net/xuVur/2/
在这里小提琴:http: //jsfiddle.net/xuVur/2/
回答by Modul8
In the example of 3 + 4 + "5" - the Javascript parser will do (3+4)+"5", as the 3+4 is first - so it will add 3 + 4 because they are numbers, then concatenate the string "5".
在 3 + 4 + "5" 的例子中 - Javascript 解析器会做 (3+4)+"5",因为 3+4 是第一个 - 所以它会加上 3 + 4,因为它们是数字,然后连接字符串“5”。
回答by UnTechie
When you add a number and a string in Javascript the result is always a string.
当您在 Javascript 中添加一个数字和一个字符串时,结果始终是一个字符串。
Check the experiment here - http://jsfiddle.net/xuVur/1/
在这里检查实验 - http://jsfiddle.net/xuVur/1/
var a = "3" + 4 + 5;
var b = 3 + 4 + "5";
$("#result1").html(a); // prints 345
$("#result2").html(b); //prints 75
回答by quantropy
It is confusing, but binary + seems to be a rare case of a number being coerced to a string. In most cases it would be the other way round (so it's very easy to think that you don't need to be too fussy about the type)
这令人困惑,但二进制 + 似乎是将数字强制转换为字符串的罕见情况。在大多数情况下,它会反过来(所以很容易认为你不需要对类型太挑剔)
4+"3" = "43"
4+"3" = "43"
4-"3" = 1
4-"3" = 1