Why does this expression return 2 in javaScript?

I would think that you can get 0, perhaps, because the lines are turned on 1's, and the operator -calls the subtraction operation?

"1" - - "1";

Thanks in advance!

+4
source share
3 answers

- prints a string to a number, and also acts as a minus sign.

1 - (-1)= 1 + 1=2

+2
source

How math works

1 - (-1) = 1 + 1
+11
source

1 - (-1) = 2. I do not see the problem? JavaScript will parse them as integers due to the minus sign, expecting math. This also happens if you multiply a number string by 1, otherwise the poor person is parseInt ().

+1
source

All Articles