When should I use double or single quotes in JavaScript?

前端 未结 30 4021
攒了一身酷
攒了一身酷 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:27

    It's mostly a matter of style and preference. There are some rather interesting and useful technical explorations in the other answers, so perhaps the only thing I might add is to offer a little worldly advice.

    • If you're coding in a company or team, then it's probably a good idea to follow the "house style".

    • If you're alone hacking a few side projects, then look at a few prominent leaders in the community. For example, let's say you getting into Node.js. Take a look at core modules, for example, Underscore.js or express and see what convention they use, and consider following that.

    • If both conventions are equally used, then defer to your personal preference.

    • If you don't have any personal preference, then flip a coin.

    • If you don't have a coin, then beer is on me ;)

提交回复
热议问题