views:

147

answers:

7

In looking up jquery examples, I see that authors tend to go with ' or " to enclose, selectors, for example.

As in:

$('#tags').click ...

or

$("#tags").click

Is this a personal style thing, or is there a reason why one is better than the other?

In my brief experience, I find that ' is faster to type. Also, building up json parameters is easier with ' because you can easily escape " in strings.

+1  A: 

Python offers exactly the same choice (single or double quotes are equivalent) and my preference, like yours, is for single quotes -- "fewer pixels" (less-cluttered appearance) is how I rationalize it;-). One exception is code that must contain lots of single quotes in string literals (names such as "O'Brian" or, more realistically, SQL query strings such as "SELECT foo FROM bar WHERE baz='zapzap'").

Alex Martelli
Um, SQL statements in JS? You scare me, sir.
annakata
Alex is talking about his Python experience.
Svitlana Maksymchuk
+4  A: 

I use ' for Javascript strings primarily. Reasoning is the the vast majority of HTML/XML markup uses " for attribute values. Some attributes contain Javascript e.g. onclick. Some of that javascript contains string literals. Hence those string literals are best delimited with '. For this reason I keep all Javascript consistent and use ' everywhere I can.

AnthonyWJones
+1 exactly my reasoning, but I like Alex's anti-pixel stance too :)
annakata
+1 although I reserve simple quotes from XML and HTML only. Since I work with other languages, I tend to be consistent and use double quotes for strings.
Vincent Robert
It is bad practice to put your javascript directly in HTML page (unobtrusive JS is better), so it would be probably mistakenly base use of ' on this practice...
Svitlana Maksymchuk
@Svitlana: "Bad practice", "mistaken"? Interesting assertions. I can't say I've ever run into serious or even mild problems with me wishing I hadn't done something like:- onclick="myFunction.call(this, 'someContext')". I can understand from a performance POV that including large swathes of Javascript in page would be bad but no Javascript at all allowed in a page? That's actually quite difficult to handle well and achieve good caching preformance whilst also avoiding the client ever using an older cached JS file not intended to work with newer HTML.
AnthonyWJones
Great comments. I think I'll stick with '. This matches up with strings in MS-SQL nicely as well.
ScottE
+1  A: 

To be consistent with java conventions I use " for strings and ' for characters (string that has length 1).

Svitlana Maksymchuk
I may start doing this, I do notice that after a day coding JS, I will constantly (mistakenly) use ' for strings in C#.
Allen
If you do programming in more than one language it is normal :) (sometimes I declare java variables with var).
Svitlana Maksymchuk
C# lets you :)
annakata
+1  A: 

I'm used to typing single quotes in PHP most of the time. Should be faster since it doesn't have to parse variables contained in single quotes. And it's also easier to type, and seems more 'clean'. So I adopted the single-quote-usage when writing JS, and as far as I know it's only a style difference.

Alec
A: 

One good reason to use " instead of ' is if you have a string literal that contains ' you can use " to save yourself from using escape characters. Vice Versa for when you have a string literal that contains ".

Hardwareguy
A: 

From the point of view of someone who occasionally uses one language to write code in another, using the single quote is preferable. For instance, when I use VB6 to drive Octave or Agena (as I was doing yesterday), I have to use a doubled double-quote to escape a single double-quote. For example,

T "CACos", CACos(CReset(-P_I, 2)), CParse(script("octave", str.Subst("printf(""%s"",num2str(acos(complex([1],[2]))));", -P_I, 2)))

Were I driving Javascript, I'd use the single quote option.

boost
+2  A: 

For Javascript, it's just preference - strings aren't processed differently as they are in some other languages, like PHP. Typically, you pick the one you won't have to escape.

JSON however, is different. It's important to remember that JSON doesn't have 100% parity with Javascript - it's a subset of Javascript whose format is more strict and limited.

And according to the spec, you delimit strings with a double-quote.

That means that

{ foo : 'bar' }

Is valid Javascript, but not valid JSON. The valid JSON for the above object is this

{ "foo" : "bar" }
Peter Bailey
Didn't know this. Great tip.
aleemb
I should also clarify that when you're actually writing literal JS objects as part of, say, an AJAX request, that doesn't mean you have to write strict JSON - although if you do it will certainly work.
Peter Bailey