Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this expression return 2 in javaScript?

Tags:

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!

like image 842
Antonio Pavicevac-Ortiz Avatar asked Dec 09 '22 03:12

Antonio Pavicevac-Ortiz


2 Answers

It's how math works

1 - (-1) = 1 + 1
like image 91
axelduch Avatar answered Dec 11 '22 08:12

axelduch


The - casts the string to a number and also acts as a minus sign.

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

like image 30
Alex Avatar answered Dec 11 '22 08:12

Alex