简体   繁体   中英

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?

"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) = 2. I dont see the issue? JavaScript will parse those as integers because of the minus sign, expecting math. It also happens if you multiply a numerical string by 1, aka the poor man's parseInt().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM