Difference between single quotes and double quotes in Javascript

JavascriptQuotes

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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.

Solution 3 - Javascript

Absolutly no difference. FREE QUOTING YEEHHAAA

Solution 4 - Javascript

> 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

Solution 5 - Javascript

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.

Solution 6 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael LumbrosoView Question on Stackoverflow
Solution 1 - Javascriptkarim79View Answer on Stackoverflow
Solution 2 - JavascriptJesse BrownView Answer on Stackoverflow
Solution 3 - JavascriptjAndyView Answer on Stackoverflow
Solution 4 - JavascriptAlex.K.View Answer on Stackoverflow
Solution 5 - JavascriptPharabusView Answer on Stackoverflow
Solution 6 - JavascriptSamView Answer on Stackoverflow