154

I know that in PHP, the only difference between double quotes and single quotes is the interpretation of variable inside a string and the treatment of escape characters.

In JavaScript, I often see double quotes used in strings. Is there a particular reason for that, or are single quotes exactly the same as double quotes?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Michael Lumbroso
  • 4,931
  • 5
  • 27
  • 34
  • 60
    The double quote requires you to press the shift key. Huge energy saver to use single quotes. :) – Tyler Sep 01 '10 at 00:02
  • 6
    @MatrixFrog There are lots of other keyboards where you have to press shift for both of them, e.g., in Germany, Hungary, Austria, etc. – totymedli Aug 19 '13 at 12:23
  • 5
    Well, actually I need to press shift for single quotes, but not for double quotes (most Turkish Q keyboards). And I abuse this by using double quotes in PHP and JavaScript except where single quotes are required/better. – Halil Özgür Dec 24 '13 at 08:59

6 Answers6

155

You'll want to use single quotes where you want double quotes to appear inside the string (e.g. for html attributes) without having to escape them, or vice versa. Other than that, there is no difference.

However, note that JSON (JavaScript Object Notation) only supports double quoted strings.

Mikko Rantalainen
  • 14,132
  • 10
  • 74
  • 112
karim79
  • 339,989
  • 67
  • 413
  • 406
  • Can u explain when to use double quotes n single quotes. Is there any diff between these two $("[href$='.jpg']"), $('[href$=".jpg"]') – Anshu Nov 21 '12 at 12:30
  • @Anshu, karim said 'or vice versa'...So he's saying its used for BOTH cases like what you have stated. – Jude Duran Jun 06 '13 at 06:22
  • 1
    @Anshu `var answer = "It's alright"; // single quote inside double quotes` `var answer = "He is called 'Johnny'"; // single quotes inside double quotes` `var answer = 'He is called "Johnny"'; // double quotes inside single quotes` – broadband Mar 05 '15 at 11:26
  • @Anshu the 'form' is that jQuery expressions use single quotes to allow using double quotes in a selector/expression, `$('div[class^="example"]')`. Text strings use double quotes, so that apostrophes can be used, `"That's why."` HTML strings tend to use single quotes for the same reason as jQuery, `''`. JSON written in Javascript can use either type, so double quotes for text, single quotes for HTML. – Orwellophile Jun 16 '15 at 12:18
  • ... and C programmers use `'c'` single quotes for characters, because that's how it's done in C. – Orwellophile Jun 16 '15 at 12:22
58

There is a difference in JSON - The JSON standard specifies that all key,value pairs should be in double quotes. (thanks to wulfgarpro in the comments), so I have started switching to using double-quotes as much as possible so that I don't make mistakes when dealing with JSON.

ffledgling
  • 11,502
  • 8
  • 47
  • 69
Jesse Brown
  • 709
  • 4
  • 4
  • 32
    Personally, I think it's much better not to try to read and write JSON yourself. I always JSON.parse and JSON.stringify, so that I'm free to use single quotes, double quotes, or even no quotes, without worrying about causing parse errors elsewhere. – Tyler Sep 01 '10 at 00:02
  • 5
    Please explain the difference in JSON? – staticboy Oct 09 '12 at 12:09
  • 19
    @staticboy - The JSON standard specifies that all key,value pairs should be in double quotes. – wulfgarpro Dec 23 '12 at 04:53
  • 2
    Only if you're writing RAW JSON. If you're writing a Javascript object, `{key: 'value'}` works exactly the same as `{"key": "value"}` and is a lot quicker to type. – Orwellophile Jun 16 '15 at 12:21
  • 5
    Calling JSON "raw" is redundant. It either is, or is not JSON. – Ben Aston Nov 22 '15 at 12:21
  • I think they mean "raw JSON" as writing the JSON string itself. JSON.stringify takes a Javascript object and turns it into JSON – Paul Stelian Jun 23 '18 at 20:03
49

Absolutly no difference. FREE QUOTING YEEHHAAA

jAndy
  • 231,737
  • 57
  • 305
  • 359
12

Unlike PHP, for which using double or single quotes changes how the string is interpreted, there is no difference in the two syntaxes in ECMAScript. A string using double quotes is exactly the same as a string using single quotes. Note, however, that a string beginning with a double quote must end with a double quote, and a string beginning with a single quote must end with a single quote.

Nicholas C. Zakas - Professional JavaScript for Web Developers

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
10

They are the same, I usually use single quotes but thats because I am a .net developer and asp.net in particular so it aids me in distinguishing between the 2 types of strings.

Pharabus
  • 6,081
  • 1
  • 26
  • 39
-6

I just found a difference. I'm making a mobile website, but I've mostly been testing on desktop Firefox. This works fine on Firefox:

var searchArray = searchValue.split(' '); // Split a string at the spaces.

BUT... it doesn't work on mobile Safari (iPhone 3GS running iOS 6.1). To make it work on mobile Safari, you have to use double quotes:

var searchArray = searchValue.split(" "); // Split a string at the spaces.

If you don't use double quotes, it doesn't split, it just puts the whole string into the first array element. That was a real puzzler for me and took quite a while to figure out; I dunno what even made me try switching the quotes, because I thought they were always supposed to act the same way. I haven't found anything on this problem by googling, so maybe this will help someone.

Sam
  • 129
  • 3
  • 12
  • 1
    FYI: I tested on an iPhone 5 / iOS 6.1 and did not see this problem. – DG. Feb 04 '13 at 14:02
  • 2
    It's highly improbable that this is true. Otherwise a *lot* of webpages would be broken on the iOS. – JJJ Mar 26 '13 at 17:43