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.
My 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.
I 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:
Some 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.
But some other languages (C/C++) have different meaning assigned to single-quoted strings. Getting in the habit of typing double quotes will ensure you will always get a string.
On 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.
I'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) which enforces single quotes. It's the style guide we use on my team.
Single 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.
I'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).
Same 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.
That's true. There's also the subtler functional difference in both languages that single quote strings can have unescaped double quotation marks in them and vice versa.