Antonio Pavicevac-Ortiz Antonio Pavicevac-Ortiz - 2 years ago 87
Javascript Question

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!

Answer Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download