16

While using gjslint, I got a hint: "Single-quoted string preferred over double-quoted string".

So why? I'm a little confused with this. Why single-quoted preferred?

Hope to get some help. Thanks all.

Lynn
  • 665
  • 8
  • 25

4 Answers4

23

It's just somebody's opinion.

Lots of people do prefer singe-quotes, but lots of other people prefer double-quotes. I tend to use double-quotes just out of habit from other languages, but I don't have a strong preference: I'm willing to change if I hear of a compelling reason why single-quotes are better, but to date I've not even heard a good reason, let alone a compelling reason.

Even the Google JavaScript Style Guide says that single-quotes are preferred without giving a good reason:

"For consistency single-quotes (') are preferred to double-quotes ("). This is helpful when creating strings that include HTML"

(It then goes on to give a very short example of a string that doesn't even include HTML.)

A lot of people seem to believe XHTML is only valid with double-quotes, but that is not true: the XHTML spec allows either. (Having said that, 98% of the XML I've run across, including XHTML, does use double-quotes.)

Within any one source file I try to stick to one or the other, but if a line of code in the middle needed a lot of embedded quotes I'd be happy to change for just that line to avoid lots of escaping in that line.

UPDATE: Just remembered that JSON is only valid with double-quotes. Obviously JSON is not JavaScript, but still that's a reason why (a) some might prefer double-quotes in their JavaScript so that object literals look like JSON, and (b) some might prefer single-quotes so that they can manually encode JSON without having to escape the double-quotes. (No, I don't recommend manually encoding JSON, but I've seen it done.)

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
15

If you use single quotes, you do not have to escape the double quotes in your html.

For example...

With single quotes you can do this

var a = '<a href="something.html" rel="yes" title="whatever">a link/a>';

with double quotes you would need to escape those inside double quotes, like so

var a = "<a href=\"something.html\" rel=\"yes\" title=\"whatever\">a link/a>";

Much more work, more difficult to maintain, and easier to commit errors.

Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
  • 24
    I can just use single quotes for HTML attributes. – BoltClock Oct 01 '11 at 04:48
  • 1
    True @BoltClock. Although I think most people who dev in HTML use dbl quotes. – Jason Gennaro Oct 01 '11 at 04:53
  • 2
    This also works the other way around, when you have Javascript strings inside attributes in the HTML (a common occurance with the templating engine I use) – hugomg Oct 01 '11 at 05:22
  • I think the point is that sometimes you need to escape your double-quotes, and sometimes you're more concerned about escaping the apostrophes. Why not just switch to whichever is most contextually convenient? I can't find any evidence in my searching today, that the prejudice for single-quotes is justified, except perhaps as a convention for distinguishing between JSON and JS. – XML Jul 09 '13 at 17:32
2

My keyboard has a '/" key so I can easily type a single quote but need to press shift for the double quote. :)

As for the JSLint warnings its just trying to enforce an arbitrary standard in order to promote a more consistent coding style, just like how it does on other spacing and indentation issues.

hugomg
  • 68,213
  • 24
  • 160
  • 246
1

In terms of performance, according with http://jsperf.com you get almost the same performance for both in some browsers, in others you get the same performance.

There's a lot of tests in jsperf and all of them get almost the same result:

In conclusion I think using single or double quotes is just a matter of taste and not a matter of performance.

Regards.

Isaac Zepeda
  • 300
  • 3
  • 10