Difference between += and =+ in javascript

后端 未结 1 1654
清歌不尽
清歌不尽 2021-01-12 10:57

I want to know why after running the third line of code the result of a is 5?

a = 10;
b = 5;
a =+ b;
相关标签:
1条回答
  • 2021-01-12 11:15

    Awkward formatting:

    a =+ b;
    

    is equivalent to:

    a = +b;
    

    And +b is just a fancy way of casting b to number, like here:

    var str = "123";
    var num = +str;
    

    You probably wanted:

    a += b;
    

    being equivalent to:

    a = a + b;
    
    0 讨论(0)
提交回复
热议问题