[英]Why does this expression return 2 in javaScript?
I would think you might get 0
, maybe because the strings are turned to 1's
and the -
operator causes a subtraction operation to take place? 我认为你可能会得到
0
,也许是因为字符串变为1's
并且-
运算符会导致减法运算?
"1" - - "1";
Thanks in advance! 提前致谢!
这就是数学的工作原理
1 - (-1) = 1 + 1
The -
casts the string to a number and also acts as a minus sign. -
将字符串转换为数字,并且还充当减号。
1 - (-1)
= 1 + 1
= 2
1 - (-1)
= 1 + 1
= 2
1 - (-1) = 2. I dont see the issue? 1 - ( - 1)= 2.我没有看到这个问题? JavaScript will parse those as integers because of the minus sign, expecting math.
由于减号,JavaScript会将它们解析为整数,期待数学。 It also happens if you multiply a numerical string by 1, aka the poor man's parseInt().
如果将数字串乘以1,也就是穷人的parseInt(),也会发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.