tags:

views:

256

answers:

5

Hi,

Are there any differences between single and double quotes in javascript?

+5  A: 

No, is the right answer most of the time. However if you want to write valid JSON, then you should use double quotes for object literals.

apphacker
+2  A: 

One is slightly wider, so you may have a few extra characters disappear to the right (as opposed to the slimmer version) in your favourite IDE.

Seriously though, I always use ', because if I need to quote a HTML element attribute, I always use " and I can't be bothered escaping like so

var html = "<a href=\"http://www.example.com\"&gt;hello&lt;/a&gt;"
alex
+1 to offset the -1. I laughed (well, on the inside anyway).
Ray
yeah, i'd upvote it too, but you must be severely punished by suggesting (even as a joke) to use proportional fonts for programming. (it's sometimes forgivable for comments, _never_ for code)
Javier
Haha, :) Fixed width font is the only way to go!
alex
+3  A: 

No, except in double quotes you can put single quotes.

e.g. "Don't not do this"

And in single quotes you can put double quotes.

e.g. 'John said "Do this"'

Ray
**except** that both your sentences say the same thing. *Don't not do this* is a double negative :)
Anurag
+2  A: 

No difference. Just make sure you close the string with whatever you open it with.

Much more sensible to me than other languages (looking at you, C# and PHP...) where single quoted strings are either character literals, or don't expand escaped characters.

Triptych
+1  A: 

you should use " always unless in an element..

<button onclick="dosomething(\"test\")">Test</button> Wont work 
<button onclick="dosomething("test")">Test</button> Wont work 

<button onclick="dosomething('test')">Test</button> will work
JamesM