When should I use double or single quotes in JavaScript?

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

    Just keep consistency in what you use. But don't let down your comfort level.

    "This is my string."; // :-|
    "I'm invincible."; // Comfortable :)
    'You can\'t beat me.'; // Uncomfortable :(
    'Oh! Yes. I can "beat" you.'; // Comfortable :)
    "Do you really think, you can \"beat\" me?"; // Uncomfortable :(
    "You're my guest. I can \"beat\" you."; // Sometimes, you've to :P
    'You\'re my guest too. I can "beat" you too.'; // Sometimes, you've to :P
    

    ECMAScript 6 update

    Using template literal syntax.

    `Be "my" guest. You're in complete freedom.`; // Most comfort :D
    

提交回复
热议问题