41

Possible Duplicate:
When to Use Double or Single Quotes in JavaScript
Are there differences between ' and "

I am wondering if there is a difference between using single quotes vs double quotes in JavaScript (JQuery selectors etc.).

Both seem to work just fine, so is there a difference?

Community
  • 1
  • 1
TGH
  • 38,769
  • 12
  • 102
  • 135

4 Answers4

67

The difference is that you don't need to escape single quotes in double quotes, or double quotes in single quotes. That is the only difference, if you do not count the fact that you must hold the Shift key to type ".

CR Drost
  • 9,637
  • 1
  • 25
  • 36
24

It doesn't matter unless you're trying to write JSON, in which case you must use double quotes for object literals.

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
BluesRockAddict
  • 15,525
  • 3
  • 37
  • 35
  • 7
    Could you please elaborate? Maybe with some examples? – Jeff Axelrod May 01 '17 at 16:01
  • 1
    @JeffAxelrod: the text `{'abc': 123}` is an example of invalid JSON. JSON refuses to delimit text like that with single quotes. (It is technically not valid JavaScript by itself, you would have to write `const x = {'abc': 123}` to make it valid JS. If you don't do that the leading `{` is parsed as a beginning of a code block.) the text `{"abc": 123}` is a valid JSON string. – CR Drost Mar 25 '19 at 19:22
12

There is no special difference between single and double quotes (like it is in PHP).

The only actual difference is that you can write a double quote " in a single-quoted string without escaping it, and vice versa.

So, if you are going to output some HTML in your JavaScript, like this:

<div id = "my_div"></div>

single quotes are more useful :-)

Imp
  • 8,409
  • 1
  • 25
  • 36
3

They work the same, but they must match. You cannot start a string with a single and end with a double, or the opposite. Other than that, they are interchangeable.

James Montagne
  • 77,516
  • 14
  • 110
  • 130