JavaScript string initialization

后端 未结 5 574
暗喜
暗喜 2021-02-04 08:39

In JavaScript, from my understanding, the below are all same:

var carter2 = new String();
var carter2 = \'\';
var carter2 = \"\";

Which one is

5条回答
  •  名媛妹妹
    2021-02-04 09:12

    While '' and "" are the same (they are primitives), new String() is not because it returns a String object.

    typeof '' == 'string'
    typeof "" == 'string'
    typeof new String() == 'object'
    

    See Distinction between string primitives and String objects.

提交回复
热议问题