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.

Eric
  • 6,563
  • 5
  • 42
  • 66
  • 2
    People are strange creatures - that's one of the things that does not have any rational reason behind it. – zerkms Oct 30 '15 at 06: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 Oct 30 '15 at 06:49
  • 1
    See http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript. –  Oct 30 '15 at 06:50
  • 1
    If you are using double quotes everywhere, vs. single quotes everywhere, why would "double quotes show up more often"? –  Oct 30 '15 at 06:51
  • @torazaburo , let say I used single quotes everywhere. As some point I would need to use them around an [tag:html] fragment which includes a lot of double quoted attributes. So now I have mostly single quotes and a bunch of double quotes. If I switched it the other way and used all double quotes except for the [tag:html] fragment which includes a lot of double quoted attributes, then I would have 2 single quotes and and all - 2 double quotes. – Eric Oct 30 '15 at 06:56
  • 1
    @Eric so if it works for you and saves 10 bytes after compression - use it. – zerkms Oct 30 '15 at 06:58
  • Personally I use single quotes for strings that the computer knows about, and double quotes for human-oriented strings, but that's just me. –  Oct 30 '15 at 07:04
  • "and use that for better handling / checking" --- what does it mean? – zerkms Oct 30 '15 at 07:15
  • @zerkms , updated the question. – Eric Oct 30 '15 at 07:44
  • `"` and `'` both occupy one character. So from minification perspective they are identical. And linters check as per how you configure them, that's the point of linters. – zerkms Oct 30 '15 at 08:01

2 Answers2

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.

Bennett Brown
  • 5,234
  • 1
  • 27
  • 35
1

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

Alexander Elgin
  • 6,796
  • 4
  • 40
  • 50