Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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?



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


Also, double quotes are in HTML attributes, and JS can be embedded in HTML attributes and vice versa (though it's less common nowadays)


HTML supports both single-quotes and double-quotes.


True, and XML as well. But double-quotes are by far the norm. (Why? I don't know... :)


JQuery uses double quotes in their style guide. That might be where it comes from.

http://contribute.jquery.org/style-guide/js/#quotes


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


And they write stringified JSON in their source code? Hm...


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


8 bit.


Thanks :). I must have been somewhere else.


The latest version of JSLint recommends using double quotes. It complains unless you use the Single option.


To me, visually, I pick up "this is a string" faster with the double quotes.




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

Search: