When should I use double or single quotes in JavaScript?

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

    Just to add my two cents: In working with both JavaScript and PHP a few years back, I've become accustomed to using single quotes so I can type the escape character ('') without having to escape it as well. I usually used it when typing raw strings with file paths, etc.

    Anyhow, my convention ended up becoming the use of single quotes on identifier-type raw strings, such as if (typeof s == 'string') ... (in which escape characters would never be used - ever), and double quotes for texts, such as "Hey, what's up?". I also use single quotes in comments as a typographical convention to show identifier names. This is just a rule of thumb, and I break off only when needed, such as when typing HTML strings '<a href="#"> like so <a>' (though you could reverse the quotes here also). I'm also aware that, in the case of JSON, double quotes are used for the names - but outside that, personally, I prefer the single quotes when escaping is never required for the text between the quotes - like document.createElement('div').

    The bottom line is, and as some have mentioned/alluded to, to pick a convention, stick with it, and only deviate when necessary.

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

    Let's look what a reference does.

    Inside jquery.js, every string is double-quoted.

    So, beginning now, I'll use double-quoted strings. (I was using single!)

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

    I would use double quotes when single quotes cannot be used and vice versa:

    "'" + singleQuotedValue + "'"
    '"' + doubleQuotedValue + '"'
    

    Instead of:

    '\'' + singleQuotedValue + '\''
    "\"" + doubleQuotedValue + "\""
    
    0 讨论(0)
  • 2020-11-21 05:17

    Single Quotes

    I wish double quotes were the standard, because they make a little bit more sense, but I keep using single quotes because they dominate the scene.

    Single quotes:

    • Airbnb
    • Facebook
    • Google
    • Grunt
    • Gulp.js
    • Node.js
    • npm (though not defined in the author's guide)
    • Wikimedia
    • WordPress
    • Yandex

    No preference:

    • Three.js

    Double quotes:

    • TypeScript
    • Douglas Crockford
    • D3.js (though not defined in .eslintrc)
    • jQuery
    0 讨论(0)
  • 2020-11-21 05:17

    If you are using JSHint, it will raise an error if you use a double quoted string.

    I used it through the Yeoman scafflholding of AngularJS, but maybe there is somehow a manner to configure this.

    By the way, when you handle HTML into JavaScript, it's easier to use single quote:

    var foo = '<div class="cool-stuff">Cool content</div>';
    

    And at least JSON is using double quotes to represent strings.

    There isn't any trivial way to answer to your question.

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

    There are people that claim to see performance differences: old mailing list thread. But I couldn't find any of them to be confirmed.

    The main thing is to look at what kind of quotes (double or single) you are using inside your string. It helps to keep the number of escapes low. For instance, when you are working with HTML content inside your strings, it is easier to use single quotes so that you don't have to escape all double quotes around the attributes.

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