4 ms·
From what I read, the author uses double quotes for string literals, "some string". Most of the style guides and linters recommend using single quotes, any ide
by paradite 10y ago
From what I read, the author uses double quotes for string literals, "some string".
Most of the style guides and linters recommend using single quotes, any idea on the advantage of each?
- grncdr 10y agoThere is no difference in JavaScript, they have the same semantics & meaning. However, single quotes have two advantages (IMO, since this is obviously subjective): 1. can be typed without shift on US keyboard layout 2. I use double quotes inside string literals (e.g. for quoting bad input in error messages) more often than single quotes (mostly used for contractions) so not having to escape double quotes by default is convenient. Standard (and I guess eslint?) support "single quotes unless a single quote appears in the string", which is ideal (for me).
- paulddraper 10y agoAlso, double quotes are in HTML attributes, and JS can be embedded in HTML attributes and vice versa (though it's less common nowadays)
- TheAceOfHearts 10y agoHTML supports both single-quotes and double-quotes.
- paulddraper 10y agoTrue, and XML as well. But double-quotes are by far the norm. (Why? I don't know... :)
- renke1 10y agoI do use double quotes in my JS projects, mostly out of habit though. One reason I can think of why people use double quotes is because JSON only supports double quotes and it's not uncommon to deal with JSON when you program in JS.
- darth_mastah 10y agoAnd they write stringified JSON in their source code? Hm...
- __mp 10y agoI prefer double quotes to single quotes to strings: When you write C and C++ a single quote generates a character 8 byte literal. A double quote is a string an array of characters.
- ChristianGeek 10y ago8 bit.
- __mp 10y agoThanks :). I must have been somewhere else.
- lloyd-christmas 10y agoJQuery uses double quotes in their style guide. That might be where it comes from. http://contribute.jquery.org/style-guide/js/#quotes http://contribute.jquery.org/style-guide/js/#quotes
- sigzero 10y agoTo me, visually, I pick up "this is a string" faster with the double quotes.
- ajeet_dhaliwal 10y agoThe latest version of JSLint recommends using double quotes. It complains unless you use the Single option.