Why does alert(“1” - - “1”); produce 2 in javascript?

后端 未结 1 355
谎友^
谎友^ 2021-01-21 19:38

When I execute the following javascript statement:

alert(\"1\" - - \"1\");

I get the result 2.

Why is this?

相关标签:
1条回答
  • 2021-01-21 20:17

    Since you can't subtract strings, the - operator converts them to numbers. If you subtract negative one from positive one you get two.

    0 讨论(0)
提交回复
热议问题