views:

1609

answers:

8

In JavaScript, it doesn't seem to matter whether you use single quotes or double quotes when writing strings. However, some programming languages treat them differently.

Is one more reliable than the other across multiple programming languages? Are there any pros or cons using one rather than the other (apart from when apostrophes or quotes are in the string)?

I would like to choose one and then be able to use it across multiple programming languages—with it meaning the same thing (if that's possible).

+6  A: 

The meaning is completely dependent on the language you are using, so what you want is not possible.

anon
+1  A: 

generally I use double quotes also in Javascript, because in the majority of the languages I usually program, strings are double quoted. in C/C++ for instance, single quote stands for a single character.

Enreeco
+2  A: 

In some scripting languages (e.g., shell, Perl, Ruby, and PHP), single-quoted strings do not support character escapes (except, in some languages, to escape the quote and backslash characters) nor variable interpolation. Double-quoted strings support both.

Example in Perl:

my $name = 'Barney';
print "Hello, $name\n";   # "Hello, Barney" (followed by newline)
print 'Hello, $name\n';   # "Hello, $name\n"

So, if you want your JavaScript to be consistent with the above, you'd probably want to use single-quoted strings---but, character escapes are still supported in single-quoted strings in JS, but not in the other languages mentioned above.

Chris Jester-Young
It's also worth mentioning that some languages don't support single quotes. Tcl comes to mind, there may be others. This isn't a limitation of Tcl, just a design choice.
Bryan Oakley
This isn't true in Python. In Python, single and double quotes are equivalent, except that you don't have to escape the other type of quote. For instance, a string containing one single quote can be written '\'', "\'", or "'".
Derek Ledbetter
Thanks Derek, I'll update my entry. :-)
Chris Jester-Young
A: 

The meaning of quotes are very language dependant. Some languages also let you use ` (backquotes) to mean something completely different from the regular ' and " quotes.

Eclipse
+8  A: 

In a lot of scripting and shell languages, there is a significant difference between double and single quotes. For example, in Perl and a lot of Unix shells, when you use double quotes, the interpreter will parse through your string and try to do any substitutions for any variables in the string. If you use single quotes, the interpreter will not try to do anything fancy with the string; it will treat it as a plain literal.

In other languages like C++ or Java, double quotes are used for strings, and single for single character literals.

Every language might treat quotes differently. I think in Javascript it might not matter which one you use, but it would be best to do some research on the best practices, then just pick one methodology and be consistent. I've seen some examples where the person chose to use double quotes in HTML and single in Javascript, but I'm not sure if that's the "standard."

Andy White
Thanks for the fixes Jonathan
Andy White
HTML (and XML) allows either style of quotes - both are standard.
anon
Javascript treats single- and double-quotes the same.
William Brendel
I think consistency is important, which might lead one to use double-quotes since it does make a difference in some languages. However, there's nothing wrong with using symbols in Ruby, so for the same reason I think it's almost compulsory to use double-quotes only when necessary. I think what I'm trying to say is, match the style of the language because two languages rarely agree.
Andrew Noyes
+1  A: 

If you're intended to move around multiple languages, getting too cosy with one or the other would probably be counter-productive. I do the majority of my development is C# (double quoted strings), but occasionally switch back to Delphi for Win32 stuff (single quoted strings). The longer I've been away from Delphi, the more mistakes I make. Strangely enough, when I'm working with both languages at once it's a little simpler!

As has been said previously, you'll have to deal with both if you do a lot of cross-language stuff, so best not to worry about it.

dommer
+3  A: 

Personally, I default to using double-quotes; it's not so much a matter of "cross-language consistency" as not having to think.

When I'm typing a string in whatever language, a double-quote works as a default in all languages I'm familiar with, but single quotes won't necessarily work.

Of course, if you're going have the string processed in some way (inline variable expansion, escaped characters or whatever) you'll need to think about and use whatever is appropriate for the language you're using.

But for the 90%+ case where "it's just a string, dammit", double quotes do the trick pretty much everywhere - at least for the languages I use. I know it's a small thing, but it's still one less thing I need to think about.

Michael Burr
SQL? -
Colin Pickard
Yeah - everyone's world is different; SQL is not a big part of mine. Of course my 'advice' will have exceptions for everyone and everywhere (as I said, "at least for the languages I use").
Michael Burr
A: 

in c the double quotes is used for set of characters.so it is used for strings. single quotes is used for to specify a single character.if the string is keep in a single quotes that is syntax error.