2

Why is there a trend for people to use single quotes ' around string instead of double quotes " when writing ? Are there any minifcation or linting tools that follow the / / convention of having double quotes around string and single quotes around single char and use that for better minification / lint checking? Or visa versa? Is it a hold over from or where double quoted strings are more expensive to execute that single quoted strings?

I have found that using double quotes generally tend to create files that gzip better (for transmission over the wire) because the double quotes show up more often, which is better for Huffman coding.

10
  • 2
    People are strange creatures - that's one of the things that does not have any rational reason behind it.
    – zerkms
    Commented Oct 30, 2015 at 6:42
  • "because the double quotes show up more often" --- that is arguable: var foo = 'bar';. In this JS there are 2 single quotes and 0 double quotes.
    – zerkms
    Commented Oct 30, 2015 at 6:49
  • 1
    See stackoverflow.com/questions/242813/….
    – user663031
    Commented Oct 30, 2015 at 6:50
  • 1
    If you are using double quotes everywhere, vs. single quotes everywhere, why would "double quotes show up more often"?
    – user663031
    Commented Oct 30, 2015 at 6:51
  • 1
    @Eric so if it works for you and saves 10 bytes after compression - use it.
    – zerkms
    Commented Oct 30, 2015 at 6:58

2 Answers 2

6

Using single quotes in JS is a convention followed so as to allow a JavaScript string to contain double quotes used (conventionally) in HTML.

5
  • 1
    "to save double quotes" --- only in companies, that have monthly quotes on quotes.
    – zerkms
    Commented Oct 30, 2015 at 6:40
  • @zerkms, sorry I had just edited out that colloquialism before seeing your comment. Commented Oct 30, 2015 at 6:41
  • 1
    And maybe the choice of using " for HTML and ' for JS is based on a desire to use single quotes in PHP. stackoverflow.com/questions/2373074/single-vs-double-quotes-vs Commented Oct 30, 2015 at 6:44
  • but why use it for strings which do not contain html?
    – Eric
    Commented Oct 30, 2015 at 6:56
  • 2
    @Eric, Consistency makes it a simple convention. Commented Oct 30, 2015 at 6:59
1

Many code conventions defined by e.g. JsHint or JsLint use single quotes (instead of double quotes) by default.

Not the answer you're looking for? Browse other questions tagged or ask your own question.