var asdf = "asdf";
vs var asdf = 'asdf';
I really haven't been doing any JS programming for quite a while now. var asdf = "asdf";
vs var asdf = 'asdf';
I really haven't been doing any JS programming for quite a while now.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.
http://stackoverflow.com/questions/3149192/difference-betwee...
http://stackoverflow.com/questions/242813/when-to-use-double...
> 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...
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).
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>"); var el = $("<a href=/ class=link>Link</a>");A string in c# is "I am a string". Single quote, is used for a char 'a' and cannot be a string.