JavaScript string initialization

后端 未结 5 565
暗喜
暗喜 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 08:54

    I prefer to use var carter2 = ''; because:

    1. It's not verbosive
    2. Double quotes might clash with attributes double-quotes inside strings.

    So I prefer to use single quotes in java-script, double quotes for html attributes.

    UPD: this is my preference, I know that single-double quotes are interchangeable.

提交回复
热议问题