Hacker News new | past | comments | ask | show | jobs | submit login

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:

http://stackoverflow.com/questions/3149192/difference-betwee...

http://stackoverflow.com/questions/242813/when-to-use-double...


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.


And Ruby


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.

[1] https://google.github.io/styleguide/javascriptguide.xml?show...

[2] http://prototypejs.org/assets/2007/1/18/prototype.js

[3] https://code.jquery.com/jquery-1.0.js


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).

[1] https://github.com/airbnb/javascript


In 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>");


In this particular case, I'd write

    var el = $("<a href=/ class=link>Link</a>");


Yeah it's a rule in a lot of style guides. Google for one example: https://google.github.io/styleguide/javascriptguide.xml?show...


I always found it bizarre that Python let you do both single and double quotes. It really goes against the whole "only one way" thing.


going 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.


Yeah for the most part I believe there is a good amount of consensus around that.


Definitely the norm in my code.


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.


In 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.


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.


Ha, that's a good point.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: