views:

413

answers:

9

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

Hello,

Do "" and '' have different meanings in JavaScript? Because I keep seeing those two usages in Jquery, for instance.

$("")

and

$('')

Thanks for helping

+8  A: 

Nope. It means the same.

Lekensteyn
+5  A: 

There is no difference between '' and "", they both represent empty strings.

BoltClock
+12  A: 

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
+13  A: 

No, they mean the same thing; they are both just JavaScript string literals. It is convenient to have two different quotes so you can nest them without having to use escape sequences; "some string with 'single quotes' in it" or 'a string with "double quotes" in it'.

Brian Campbell
Also, some programmers use both `'` and `"` purely out of habit from other languages that do distinguish them.
dan04
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
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: `<a href="#" onclick="do_something(\"some argument\"); return false;">`
Gumbo
@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
@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
@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
+5  A: 

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
+5  A: 

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
+4  A: 

stackoverflow is searchable.

patrick dw
A: 

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

jojaba
A: 

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

Kira