When should I use double or single quotes in JavaScript?

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

    Now that it's 2020, we should consider a third option for JavaScript: The single backtick for everything.

    This can be used everywhere instead of single or double quotes.

    It allows you to do all the things!

    1. Embed single quotes inside of it: `It's great!`

    2. Embed double quotes inside of it: `It's "really" great!`

    3. Use string interpolation: `It's "${better}" than great!`

    4. It allows multiple lines: `

      This

      Makes

      JavaScript

      Better!

    `

    It also doesn't cause any performance loss when replacing the other two: Are backticks (``) slower than other strings in JavaScript?

提交回复
热议问题