When should I use double or single quotes in JavaScript?

前端 未结 30 3446
攒了一身酷
攒了一身酷 2020-11-21 05:02

console.log("double"); vs. console.log(\'single\');

I see more and more JavaScript libraries out there using single quotes when ha

30条回答
  •  天涯浪人
    2020-11-21 05:28

    Talking about performance, quotes will never be your bottleneck. However, the performance is the same in both cases.

    Talking about coding speed, if you use ' for delimiting a string, you will need to escape " quotes. You are more likely to need to use " inside the string. Example:

    // JSON Objects:
    var jsonObject = '{"foo":"bar"}';
    
    // HTML attributes:
    document.getElementById("foobar").innerHTML = '';
    

    Then, I prefer to use ' for delimiting the string, so I have to escape fewer characters.

提交回复
热议问题