When should I use double or single quotes in JavaScript?

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

    If you're dealing with JSON, it should be noted that strictly speaking, JSON strings must be double quoted. Sure, many libraries support single quotes as well, but I had great problems in one of my projects before realizing that single quoting a string is in fact not according to JSON standards.

    0 讨论(0)
  • 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?

    0 讨论(0)
  • 2020-11-21 05:20

    After reading all the answers that say it may be be faster or may be have advantages, I would say double quotes are better or may be faster too because the Google Closure compiler converts single quotes to double quotes.

    0 讨论(0)
  • 2020-11-21 05:23

    If you're jumping back an forth between JavaScript and C#, it's best to train your fingers for the common convention which is double quotes.

    0 讨论(0)
  • 2020-11-21 05:25

    There is no one better solution; however, I would like to argue that double quotes may be more desirable at times:

    • Newcomers will already be familiar with double quotes from their language. In English, we must use double quotes " to identify a passage of quoted text. If we were to use a single quote ', the reader may misinterpret it as a contraction. The other meaning of a passage of text surrounded by the ' indicates the 'colloquial' meaning. It makes sense to stay consistent with pre-existing languages, and this may likely ease the learning and interpretation of code.
    • Double quotes eliminate the need to escape apostrophes (as in contractions). Consider the string: "I'm going to the mall", vs. the otherwise escaped version: 'I\'m going to the mall'.
    • Double quotes mean a string in many other languages. When you learn a new language like Java or C, double quotes are always used. In Ruby, PHP and Perl, single-quoted strings imply no backslash escapes while double quotes support them.

    • JSON notation is written with double quotes.

    Nonetheless, as others have stated, it is most important to remain consistent.

    0 讨论(0)
  • 2020-11-21 05:25

    Technically there's no difference. It's only matter of style and convention.

    Douglas Crockford recommends using single quotes for internal strings and double quotes for external (by external we mean those to be displayed to user of application, like messages or alerts).

    I personally follow that.

    UPDATE: It appears that Mr. Crockford changed his mind and now recommends using double quotes throughout :)

    0 讨论(0)
提交回复
热议问题