Javascript String Assignment Operators

后端 未结 6 505
执笔经年
执笔经年 2021-01-17 17:12

How come I can use += on a string, but I cannot use -= on it?

For example...

var test = \"Test\";
var arr = \"⇔\"         


        
6条回答
  •  执笔经年
    2021-01-17 17:28

    The short answer is - it isn't defined to work with strings.

    Longer answer: if you try the subtraction operator on two strings, it will first cast them to numbers and then perform the arithmetic.

    "10" - "2" = 8
    

    If you try something that is non-numeric, you get a NaN related error:

    "AA" - "A" = NaN
    

提交回复
热议问题