1

All coding "tutorials" I've used so far use single quotes (' '), but they need to escape apostrophes using a \. So I've switched to use double quotes (" ") because they work just as well without the need to escape special punctuation signs.

Is there a difference how JavaScript or jQuery interprets the string depending on the type of quote I use? Is there a speed or possible syntax issues with using one over another?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
unbindall
  • 514
  • 1
  • 13
  • 29
  • no, they are the same to js. not to JSON, but to JS. you can always use both anywhere. if you want to avoid both in multi-line blocks, you need to use a comment inside a function. – dandavis Jan 28 '15 at 00:19
  • 1
    They are 100% the same. The only thing you can't do is to mix n match the two quotes, like `"foo' + 'bar"` – Derek 朕會功夫 Jan 28 '15 at 00:32

2 Answers2

5

They are the same. But your statement:

So I've switched to use double quotes because they work just as well without the need to escape special punctuation signs.

is incorrect, because you must escape double quotes too when used in a string, for example:

quote = "\"The early bird gets the worm\""

If you want to avoid this, use a combination of both:

quote = '"The early bird gets the worm"'
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • Really, JavaScript is irrelevant, but it is surely the same. – Malik Brahimi Jan 28 '15 at 00:23
  • So I get that I have to use them interchangeably when using quotes inside a quote, but... you don't really mention whether one is semantically better than the other. – unbindall Jan 28 '15 at 00:25
  • I prefer single quotes, more aesthetic in my opinion and easier to embed double quotes within. See the JavaScript tutorial [here](http://www.w3schools.com/js/js_strings.asp) for more info. – Malik Brahimi Jan 28 '15 at 00:26
0

There is no logical difference in JavaScript syntax. In jQuery I mostly use '....' so that I can use the double quotes for my HTML syntax that is included in the jQuery as parameters without having to escape the extra double quotes.

mehmetseckin
  • 3,061
  • 1
  • 31
  • 43
Dan Randolph
  • 741
  • 8
  • 14