11

Possible Duplicate:
When should I use double or single quotes in JavaScript?

Do "" and '' have different meanings in JavaScript?

Because I keep seeing those two usages in jQuery, for instance:

$("")

and

$('')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richard77
  • 20,343
  • 46
  • 150
  • 252
  • 1
    Duplicate? http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript **and** http://stackoverflow.com/questions/3149192/difference-between-single-quotes-and-double-quotes-in-javascript **and** http://stackoverflow.com/questions/559752/single-quotes-versus-double-quotes-in-js – user113716 Aug 25 '10 at 16:46
  • Note: The title renders differently from the body, but the title (as the body) actually contains 2 x double quote and 2 x single quote (ASCII). – Peter Mortensen Jun 26 '20 at 21:09

7 Answers7

14

Read about strings in JavaScript. There is no difference.

But as HTML properties are often defined with double-quotes, I would use single-quotes, which makes code like

$('<a href="someurl" />') 

easier to write.

Use the one with which you have less characters to escape inside the string.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
14

No, they mean the same thing; they are both just JavaScript string literals.

Having have multiple different quote styles is useful so that:

  • You can nest quotes without having to use escape sequences eg. "some string with 'single quotes' in it", or 'a string with "double quotes" in it', and
  • JavaScript strings can be conveniently used inside directly inside HTML, where double-quotes have a special meaning eg <button onclick="alert('foo')">Click me</div>
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • 1
    Also, some programmers use both `'` and `"` purely out of habit from other languages that do distinguish them. – dan04 Aug 25 '10 at 16:26
  • As Brian Campbell explained they are the same but they also provide a great way of saving you from escaping quotes, so: - "some string with \"single quotes\" in it" becomes: - "some string with 'single quotes' in it" as he points out. I tend to abuse single quotes because in my keyboard layout single quote doesn't require the shift key to be down so they are faster to add and less bulky to read. =P – Chepech Aug 25 '10 at 16:54
  • Your HTML example is wrong. Here you’re in the HTML context and not in the JavaScript context. Otherwise the following would work but it doesn’t: `` – Gumbo Aug 25 '10 at 17:20
  • @Gumbo How is my HTML example wrong? You can use single quotes to quote JavaScript strings inside of an HTML attribute. My point was that if you only had double quotes in JavaScript, you'd have to do something cumbersome like `onlclick="do_something("some argument"); return false;"`; since HTML uses only double quotes, having single quotes available in JavaScript is helpful. – Brian Campbell Aug 25 '10 at 18:45
  • @Brian Campbell: Your example is not wrong but rather misleading. Because even if JavaScript just had double quotes as delimiters for strings, you could still use single quotes to wrap the HTML attribute value `onlclick='do_something("some argument");'`. – Gumbo Aug 25 '10 at 18:57
  • @Gumbot Oh, sorry. I had completely spaced and forgotten that single quotes are legal HTML attribute delimiters as well. I'll delete that example. – Brian Campbell Aug 25 '10 at 19:18
8

Nope. It means the same.

Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
5

They both are string delimiters. The only difference is if you can use " to enclose a string with ' in it, and you can use ' to enclose a string with " in it.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
5

You can use either. I recommend sticking to one standard though throughout your project, things can sometimes get a little messy when interchanging between them when combining with server side code.

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
0

Outside of a string literal, No. Inside of a string literal, Yes.

tidwall
  • 6,881
  • 2
  • 36
  • 47
0

No... since isn't possible use "" inside "", the "" and '' make a good combination when need quote a string inside another.

Kira
  • 608
  • 3
  • 10
  • 22