When should I use double or single quotes in JavaScript?

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

    It's mostly a matter of style and preference. There are some rather interesting and useful technical explorations in the other answers, so perhaps the only thing I might add is to offer a little worldly advice.

    • If you're coding in a company or team, then it's probably a good idea to follow the "house style".

    • If you're alone hacking a few side projects, then look at a few prominent leaders in the community. For example, let's say you getting into Node.js. Take a look at core modules, for example, Underscore.js or express and see what convention they use, and consider following that.

    • If both conventions are equally used, then defer to your personal preference.

    • If you don't have any personal preference, then flip a coin.

    • If you don't have a coin, then beer is on me ;)

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

    One (silly) reason to use single quotes would be that they don't require you to hit the shift key to type them, whereas a double quote do. (I'm assuming that the average string doesn't require escaping, which is a reasonable assumption.) Now, let's suppose every day I code 200 lines of code. Maybe in those 200 lines I have 30 quotes. Maybe typing a double quote takes 0.1 seconds more time than typing a single quote (because I have to hit the shift key). Then on any given day, I waste 3 seconds. If I code in this manner for 200 days a year for 40 years, then I've wasted 6.7 hours of my life. Food for thought.

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

    Talking about performance, quotes will never be your bottleneck. However, the performance is the same in both cases.

    Talking about coding speed, if you use ' for delimiting a string, you will need to escape " quotes. You are more likely to need to use " inside the string. Example:

    // JSON Objects:
    var jsonObject = '{"foo":"bar"}';
    
    // HTML attributes:
    document.getElementById("foobar").innerHTML = '<input type="text">';
    

    Then, I prefer to use ' for delimiting the string, so I have to escape fewer characters.

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

    I hope I am not adding something obvious, but I have been struggling with Django, Ajax, and JSON on this.

    Assuming that in your HTML code you do use double quotes, as normally should be, I highly suggest to use single quotes for the rest in JavaScript.

    So I agree with ady, but with some care.

    My bottom line is:

    In JavaScript it probably doesn't matter, but as soon as you embed it inside HTML or the like you start to get troubles. You should know what is actually escaping, reading, passing your string.

    My simple case was:

    tbox.innerHTML = tbox.innerHTML + '<div class="thisbox_des" style="width:210px;" onmouseout="clear()"><a href="/this/thislist/'
                       + myThis[i].pk +'"><img src="/site_media/'
                       + myThis[i].fields.thumbnail +'" height="80" width="80" style="float:left;" onmouseover="showThis('
                       + myThis[i].fields.left +','
                       + myThis[i].fields.right +',\''
                       + myThis[i].fields.title +'\')"></a><p style="float:left;width:130px;height:80px;"><b>'
                       + myThis[i].fields.title +'</b> '
                       + myThis[i].fields.description +'</p></div>'
    

    You can spot the ' in the third field of showThis.

    The double quote didn't work!

    It is clear why, but it is also clear why we should stick to single quotes... I guess...

    This case is a very simple HTML embedding, and the error was generated by a simple copy/paste from a 'double quoted' JavaScript code.

    So to answer the question:

    Try to use single quotes while within HTML. It might save a couple of debug issues...

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-21 05:33

    I've been running the following about 20 times. And it appears that double quotes are about 20% faster.

    The fun part is, if you change part 2 and part 1 around, single quotes are about 20% faster.

    //Part1
    var r='';
    var iTime3 = new Date().valueOf();
    for(var j=0; j<1000000; j++) {
        r+='a';
    }
    var iTime4 = new Date().valueOf();
    alert('With single quote : ' + (iTime4 - iTime3));  
    
    //Part 2                
    var s="";
    var iTime1 = new Date().valueOf();
    for(var i=0; i<1000000; i++) {
        s += "a";
    }
    var iTime2 = new Date().valueOf();
    alert('With double quote: ' + (iTime2 - iTime1));
    
    0 讨论(0)
提交回复
热议问题