Difference between single quotes and double quotes in Javascript [duplicate]

Asked
Active3 hr before
Viewed126 times

7 Answers

duplicatedifferencesingle
90%

A double-quoted string can have single quotes without escaping them, conversely, a single-quoted string can have double quotes within it without having to escape them., Double quotes ( \" ) must escape a double quote and vice versa single quotes ( \' ) must escape a single quote.,On German, Hungarian, Austrian, and many other keyboards, you have to use the Shift key for both single or double-quotes.,Combing through a few popular JavaScript projects we can see a clear preference for single quotes over double-quotes.


'apple' === "apple"

'apple' //correct
"apple" //correct
"apple' //incorrect

var div = '<div class="panel">...</div>'

var message = 'Javascript'
s beauty is simplicity ';

var message = "Javascript's beauty is simplicity";

var message = 'Javascript\'s beauty is simplicity';
load more v
88%

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.,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., 58 The double quote requires you to press the shift key. Huge energy saver to use single quotes. :) – MatrixFrog Sep 1 '10 at 0:02 ,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?

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.
load more v
72%

The quotes rule can also enforce one type of quote except when the string contains a quote character that would have to be escaped otherwise.,It comes down to personal preference, though many people lobby for picking one and using it consistently when creating JavaScript strings.,Either two double "" or single '' quote marks next to each other can represent an empty string containing no characters at all.,There is only one difference between the use of single quotes ('') and double quotes ("") in JavaScript, and it comes down to which quote character you have to escape using the backslash (\) character: \' or \".

load more v
65%

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.,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.,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?,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.

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.
load more v
75%

While in a double quoted string can have single quotes without escaping.,There is no difference between using single or double quotes, meaning they both represent a string in the end. The other character within a string no need to escape., In the single quoted string can have double quotes within it without having to escape.,When to use double or single quotes in javascript ?

console.log("double");
vs console.log('single');
load more v
40%

There is only one difference in the usage of single and double quotes, and it comes down to what quote character you need to escape using the backslash character (\): \’ or \”. Each type of quote should escape its own type.,But there is no need to escape the other character inside a string. Hence, a double quote can have single quotes without escaping them, and vice versa.,Generally, there is no difference between using double or single quotes, as both of them represent a string in the end.,On the contrary, JSON allows using double quotes only. Also, using double quotes, you get rid of apostrophes while writing English letters.

"double quotes ( \" ) should escape a double quote"
'single quotes ( \' ) should escape a single quote'
load more v
22%

What is the difference between single-quoted and double-quoted strings in PHP?,Single or double quotes in PHP programming are used to define a string. But, there are lots of differences between these two.,Below program illustrates the Double-quoted Strings:Program 2:,Web 1.0, Web 2.0 and Web 3.0 with their difference

load more v

Other "duplicate-difference" queries related to "Difference between single quotes and double quotes in Javascript [duplicate]"