When should I use double or single quotes in JavaScript?

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

    If your JavaScript source is

    elem.innerHTML="<img src='smily' alt='It\'s a Smily' style='width:50px'>";
    

    the HTML source will be:

    <img src="smiley" alt="It's a Smiley" style="width:50px">
    

    Or for HTML5

    <img src=smiley alt="It's a Smiley" style=width:50px>
    

    JavaScript allows arrays like that:

    var arr=['this','that'];
    

    But if you stringify it, it will be for compatibility reasons:

    JSON=["this","that"]
    

    I'm sure this takes some time.

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

    Section 7.8.4 of the specification describes literal string notation. The only difference is that DoubleStringCharacter is "SourceCharacter but not double-quote" and SingleStringCharacter is "SourceCharacter but not single-quote". So the only difference can be demonstrated thusly:

    'A string that\'s single quoted'
    
    "A string that's double quoted"
    

    So it depends on how much quote escaping you want to do. Obviously the same applies to double quotes in double quoted strings.

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

    When using CoffeeScript I use double quotes. I agree that you should pick either one and stick to it. CoffeeScript gives you interpolation when using the double quotes.

    "This is my #{name}"
    

    ECMAScript 6 is using back ticks (`) for template strings. Which probably has a good reason, but when coding, it can be cumbersome to change the string literals character from quotes or double quotes to backticks in order to get the interpolation feature. CoffeeScript might not be perfect, but using the same string literals character everywhere (double quotes) and always be able to interpolate is a nice feature.

    `This is my ${name}`
    
    0 讨论(0)
  • 2020-11-21 05:11

    There isn't any difference between single and double quotes in JavaScript.

    The specification is important:

    Maybe there are performance differences, but they are absolutely minimum and can change any day according to browsers' implementation. Further discussion is futile unless your JavaScript application is hundreds of thousands lines long.

    It's like a benchmark if

    a=b;
    

    is faster than

    a = b;
    

    (extra spaces)

    today, in a particular browser and platform, etc.

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

    There is strictly no difference, so it is mostly a matter of taste and of what is in the string (or if the JavaScript code itself is in a string), to keep number of escapes low.

    The speed difference legend might come from PHP world, where the two quotes have different behavior.

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

    If you're doing inline JavaScript (arguably a "bad" thing, but avoiding that discussion) single quotes are your only option for string literals, I believe.

    E.g., this works fine:

    <a onclick="alert('hi');">hi</a>
    

    But you can't wrap the "hi" in double quotes, via any escaping method I'm aware of. Even &quot; which would have been my best guess (since you're escaping quotes in an attribute value of HTML) doesn't work for me in Firefox. " won't work either because at this point you're escaping for HTML, not JavaScript.

    So, if the name of the game is consistency, and you're going to do some inline JavaScript in parts of your application, I think single quotes are the winner. Someone please correct me if I'm wrong though.

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