Javascript字符串赋值运算符 [英] Javascript String Assignment Operators
本文介绍了Javascript字符串赋值运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么我可以在字符串上使用 + =
,但我不能在其上使用 - =
?
How come I can use +=
on a string, but I cannot use -=
on it?
例如......
var test = "Test";
var arr = "⇔"
test += arr;
alert(test); // Shows "Test⇔"
test -= arr;
alert(test); // Shows "NaN"
推荐答案
简短的回答是 - 它没有被定义为使用字符串。
The short answer is - it isn't defined to work with strings.
更长的答案:如果你在两个字符串上尝试减法运算符,它将首先将它们转换为数字,然后执行算术运算。
Longer answer: if you try the subtraction operator on two strings, it will first cast them to numbers and then perform the arithmetic.
"10" - "2" = 8
如果您尝试非数字化的东西,则会收到与NaN相关的错误:
If you try something that is non-numeric, you get a NaN related error:
"AA" - "A" = NaN
这篇关于Javascript字符串赋值运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文