5 ms·
I disagree on the usage of double quotes over single quotes for strings. Single quotes are useful when the string contains HTML. The Google JavaScript Style gu
by fuzionmonkey 15y ago
I disagree on the usage of double quotes over single quotes for strings. Single quotes are useful when the string contains HTML.
The Google JavaScript Style guide recommends single quotes over double quotes for this reason:
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Strings#Strings http://google-styleguide.googlecode.com/svn/trunk/javascript...
- taitems 15y agoIt's interesting that Google and jQuery seem to disagree on this as per: http://docs.jquery.com/JQuery_Core_Style_Guidelines#Strings http://docs.jquery.com/JQuery_Core_Style_Guidelines#Strings I'm yet to see a compelling argument for either. My original paragraph for string creation forgave C style strings, as they do seem very clean cut and easy to identify. I think this is an area that needs some debate.
- fuzionmonkey 15y agoI think the idea is that if you use single quotes for strings, you don't have to escape the double quotes that are often found in html. '<p id="foo">something</p>' But the same goes for something like "you can't do that" I guess it just depends what you don't want to escape. For Google, the more probable scenario was probably the former.
- julian37 15y agoThe former isn't a very good reason: HTML/XML accept both double and single quotes, so "<p id='foo'>something</p>" would work equally well.
- joshontheweb 15y agoyes, but it is much more common for double quotes to be used in html
- roel_v 15y agoSingle quotes are not 'C-style strings'. The article is wrong on strings in C. In C, strings and characters are different things (well actually, strings don't even really exist - they are pointers to the beginning of a char-array). So you can say char a1 = 'a'; and char* a2 = "a"; and they're very different things; for one, a2 has a terminating zero. (meaning: the first influences only the contents of a single byte, the second one two, leaving aside alignment). In C: single quote = character, double-quote = easy initialization of array of characters terminated with \0. (the above contains many simplifications, of course)
- tremendo 15y agoMy favorite reason for using single quotes whenever possible, is that it doesn't require two fingers (Shift for double quotes), so it's easy on my fingers and faster, and it's also easier (for me) when switching languages, as normally you can use string interpolation when using double quotes. Similarly I find myself favoring += 1 for increments over ++ as the latter doesn't work in Ruby.