3

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

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

Community
  • 1
  • 1
  • 2
    Hey, @homey1, I think you need to accept an answer :) ( This question / post is getting quite old, still without a green tick :) – JamesM-SiteGen May 20 '11 at 07:30

5 Answers5

7

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.

Bjorn
  • 69,215
  • 39
  • 136
  • 164
5

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
  • 45,695
  • 27
  • 126
  • 169
3

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\">hello</a>"
alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    +1 to offset the -1. I laughed (well, on the inside anyway). – Ray Feb 18 '09 at 03:57
  • 1
    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 Feb 18 '09 at 04:03
  • 1
    Haha, :) Fixed width font is the only way to go! – alex Feb 18 '09 at 04:26
  • @Javier: Do you realize that you can be more productive using variable width because there is less scrolling **:)** (+1 for variable width) – 700 Software May 18 '11 at 19:16
3

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.

Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
2

You would mostly use " unless in a position in where you can't escape it. ( It is neater to most developers, Don't ask why )
Also "$myVar" in php will allow the string to have the variables value. ( I know its not javascript, but another example.. In bash,

   echo "What is your name?\nMy name is $(whoami)."

will run the function / command whoami.

<button onclick="dosomething(\"test\")">Test</button> Won't work 
<button onclick="dosomething("test")">Test</button> Won't work 
<section id='"Where-As">
    <button onclick="dosomething('test')">Test</button>
    <!-- will work -->
</section>

P.S: Valid JSON objects should be using double quotes.

Other fun with different quotes:

console.log('\n\n\n\n');  // Will give you \n\n\n\n as a string.
console.log("\n\n\n\n"); // Will give lines.
Community
  • 1
  • 1
JamesM-SiteGen
  • 802
  • 2
  • 11
  • 26
  • However, if you **are** ***obsessed*** with double quotes, you can use `onclick="dosomething("test")"` – 700 Software May 18 '11 at 19:18
  • Seriously though, ***why*** should someone "always" use double quotes? (for normal circumstances / ignoring my joke) – 700 Software May 18 '11 at 19:19
  • @george: No one needs double quotes, just usually prefure " over ' as it looks nicer to lots of people. ( No change in the binary length, so worthless requirement :) – JamesM-SiteGen May 20 '11 at 07:23