When should I use double or single quotes in JavaScript?

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

    Strictly speaking, there is no difference in meaning; so the choice comes down to convenience.

    Here are several factors that could influence your choice:

    • House style: Some groups of developers already use one convention or the other.
    • Client-side requirements: Will you be using quotes within the strings? (See Ady's answer.)
    • Server-side language: VB.NET people might choose to use single quotes for JavaScript so that the scripts can be built server-side (VB.NET uses double-quotes for strings, so the JavaScript strings are easy to distinguished if they use single quotes).
    • Library code: If you're using a library that uses a particular style, you might consider using the same style yourself.
    • Personal preference: You might think one or other style looks better.

提交回复
热议问题