6 ms·
Are single quotes the preferred way of making strings nowadays then? It seems to be pretty common among new JS frameworks/libraries source code that I see nowad
by ghrifter 11y ago
Are single quotes the preferred way of making strings nowadays then? It seems to be pretty common among new JS frameworks/libraries source code that I see nowadays.
var asdf = "asdf";
vs
var asdf = 'asdf';
I really haven't been doing any JS programming for quite a while now.
- chromedude 11y agoYeah for the most part I believe there is a good amount of consensus around that.
- ShakataGaNai 11y agoI suspect it's mostly preference & less keystrokes (single tick requires no shift key). All the answers on stack overflow seem to indicated there's not really a difference: http://stackoverflow.com/questions/3149192/difference-between-single-quotes-and-double-quotes-in-javascript http://stackoverflow.com/questions/3149192/difference-betwee... http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript http://stackoverflow.com/questions/242813/when-to-use-double...
- whoopdedo 11y agoSome languages (Perl and PHP) will interpolate variables in double-quoted strings. Getting in the habit of typing single-quotes defends against accidentally using variable substitution when you don't mean to.
- batuhanicoz 11y agoOn my Turkish Q keyboard, double quotes are the one keystroke and single quote is shift+2. Since I'm lazy I keep using double quotes when writing JavaScript.
- 49531 11y agoI'm not exactly sure why, but that seems to be the case. It might be due to the ubiquitous AirBnB JS Style guide (https://github.com/airbnb/javascript https://github.com/airbnb/javascript) which enforces single quotes. It's the style guide we use on my team.
- zeven7 11y agoSingle quotes have been mainstream in JS for much longer than AirBnB has been around. Take for example, the Google style guide[1]: > For consistency single-quotes (') are preferred to double-quotes ("). This is helpful when creating strings that include HTML Or take the oldest version of Prototype JS I could find (a JS library from the old days), version 1.5 from Jan 2007[2]. A simple search of the source shows 690 occurrences of ' and 48 occurrences of ". jQuery 1.0, dated 2006[3] is a counter example which has many more occurrences of double quotes, but I believe single quotes were generally more common even before that. I think, in addition to the HTML thing mentioned already, PHP also had a lot to do with it, since single quotes would be encouraged in PHP when you just want a plain string. [1] https://google.github.io/styleguide/javascriptguide.xml?showone=Strings#Strings https://google.github.io/styleguide/javascriptguide.xml?show... [2] http://prototypejs.org/assets/2007/1/18/prototype.js http://prototypejs.org/assets/2007/1/18/prototype.js [3] https://code.jquery.com/jquery-1.0.js https://code.jquery.com/jquery-1.0.js
- khalilravanna 11y agoYeah it's a rule in a lot of style guides. Google for one example: https://google.github.io/styleguide/javascriptguide.xml?showone=Strings#Strings https://google.github.io/styleguide/javascriptguide.xml?show...
- sushid 11y agoI'm not sure why/when the shift took place, but I do like it because it looks a lot cleaner than double quotes. At work I use airbnb's linting configuration as shown here[1], which specifies the usage of single quotes for strings (no explanation for the preference though). [1] https://github.com/airbnb/javascript https://github.com/airbnb/javascript
- __derek__ 11y agoIn speculating on reasons behind the preference, my best guess is HTML. It seems more natural/idiomatic to write: var el = $('<a href="/" class="link">Link</a>'); rather than var el = $("<a href='/' class='link'>Link</a>"); let alone var el = $("<a href=\"/\" class=\"link\">Link</a>");
- exprx 11y agoIn this particular case, I'd write var el = $("<a href=/ class=link>Link</a>");
- RodericDay 11y agoI always found it bizarre that Python let you do both single and double quotes. It really goes against the whole "only one way" thing.
- user_0001 11y agogoing to python from c# I noticed this. A string in c# is "I am a string". Single quote, is used for a char 'a' and cannot be a string.
- savanaly 11y agoDefinitely the norm in my code.
- baddox 11y agoSame here, in both JavaScript and Ruby. I guess single quotes are easier to type on standard keyboard setups, but I have never been a fan for some reason.
- __derek__ 11y agoIn Ruby, at least, there's a functional difference. Using single-quotes is a good way to indicate that a thing is just a string and not a template.
- kbenson 11y agoMy assumption is that for JS single quotes are used because double quotes are the standard for HTML, and this allows for easier use of JS code in HTML attributes for JS events (e.g. onclick="alert('foo');"). I assume JS allows both because of the languages that came before, such as Perl, where it was a convenience to make it easier to include either double quotes or single quotes (contractions) within a string without escaping (but Perl goes much farther than that with the q and qq operators). In other languages, such as C/C++ single quotes aren't used for string, but for character literals.