Javascript String Assignment Operators

后端 未结 6 507
执笔经年
执笔经年 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:33

    Because the + operator concatenates strings, but the - operator only subtracts numbers from each other.

    As to the why -- probably because it is difficult to determine what people want to do when they subtract strings from each other.

    For example:

    "My string is a very string-y string" - "string"
    

    What should this do?

提交回复
热议问题