2

Possible Duplicate:
When to Use Double or Single Quotes in JavaScript

In Javascript, you can either use double quotes or single quotes around strings, and it means exactly the same thing:

var a = 'hello';
var b = "hello";
alert(a === b); //alerts true

I was reading some of Douglas Crockford's code, and it looks like he usually uses single quotes around string literals.

Further, most of the high rep people who answer Javascript questions around here seem to use single quotes around strings (example).

Since I usually use double quotes around strings (mostly just out of habit), I'm starting to feel self-conscious about all the answers I've given here that use double quotes. I haven't heard of any particular reason why one should use one over the other, since, as far as I can tell, they are interchangeable.

Is there a reason why all of the Javascript ninjas use single quotes? Should I use single quotes too? Will there be some unexpected fatal consequences if I continue using double quotes?

Community
  • 1
  • 1
Peter Olson
  • 139,199
  • 49
  • 202
  • 242

3 Answers3

1

Strings ideally are enclosed in double quotes. There are cases where you want to use single quotes when you're wrapping text that has double quotes within them like:

var txt = 'This "should" be escaped';

Without single quotes, you'd need to escape the double quotes which makes them bit ugly (matter of taste though).

If you're in ASP.Net world, usually to workaround the nastiness of DataBinder expression, you wrap strings in single quotes.

Plus I find single quotes easier to type.

Another place where you must use double quotes is constructing JSON objects:

var obj = { "a": 1 }; // valid
var obj = { 'a': 1 }; // invalid

Either way, being consistent is most important. Choose one style and stick to it throughout the code (except when you run into escaping issues).

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • But you have the same problem with single quotes if you have single quotes inside of the string. E.g. with apostrophes. – Peter Olson Aug 31 '11 at 15:19
  • 1
    Bingo! Now you know when to use double quotes! :) – Mrchief Aug 31 '11 at 15:23
  • "Strings ideally are enclosed in double quotes." - a matter of opinion I think. In terms of JavaScript I prefer single quotes, since I favour double quotes for the HTML and the two are often used together. – MrWhite Aug 31 '11 at 15:29
  • @w3d: Not really if you consider other programming languages. That is why I didn't say "Strings in _Javascript_...". It also makes copy/pasting code from/to other languages easy. Personally I prefer single quotes too as they are a bit easier to type. – Mrchief Aug 31 '11 at 15:37
0

I believe that is is a primarily stylistic choice, however when it comes to quote or string escaping it can affect your string.

Overall, ensure that you are consistent whichever method you chose.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
0

You can choose either one, the main thing is to be consistant.

They can be used interchangably however most languges such as c# " needs to be escaped however ' does not so its slightly simpler to use '

Tom Squires
  • 8,848
  • 12
  • 46
  • 72