views:

210

answers:

4

Possible Duplicates:
single quotes versus double quotes in js
When to Use Double or Single Quotes in JavaScript

What is the difference (if any) between the javascript strings defined below?

var str1 = "Somestring";

var str2 = 'Somestring';

"" and '' mean two very different things to me predominantly writing code in C++ :-)

EDIT: If there is no difference why are there two ways of achieving the same thing and which is considered better practice to use and why. Thanks!

+2  A: 

No difference at all.

Seb
+12  A: 

Javascript treats single and double quotes as string delimiters.

If you use single quotes, you can use double quotes inside the string without escaping them.

If you use double quotes, you can use single quotes inside the string without escaping them.

Both examples evaluate to the same thing.

alert(str1 == str2); // true
alert(str1 === str2); // true

Why two ways? Due to the way javascript allows you to mix the two, you can write html attributes out without messy escapes:

var htmlString1 = "<a href='#'>link</a>";
var htmlString2 = '<a href="#">link</a>';

As for best practice, there is no convention. Use what feels best.

Personally, I like making sure the Javascript I emit matches the HTML (if I double quote attributes, I will delimit JS string with a ', so emitted attributes will use ").

Oded
Why the downvote?
Oded
Ill upvote you....Its a good explination and an excelent test.
John Hartsock
@Oded crap I had upvoted you but I just accidentally clicked the arrow again and now I'm locked out. If you edit I'll upvote again. Sorry about that. (I also wonder what the point of the downvote was.)
Pointy
@Pointy - Edited some more ;)
Oded
Good answer, +1 and accepted. Thanks!
Konrad
I think it should be pointed out though that for real valid JSON you should use the double quote: `{"property":"string Value"}` is considered valid JSON while `{'property':'string value'}` is not. Even though JavaScript won't have a problem with the later, I have seen some JSON parser crashing when using single quotes and JSONLint will report is as invalid.
SBUJOLD
+2  A: 

I believe the answer is there is no difference. They are both strings.

Here would be the usage of '

var mynewhtml = '<body class="myclass" ></body>';

or using "

var mynewhtml = "<body class='myclass' ></body>";

this also works but IMO is harder to read

var mynewhtml = "<body class=\"myclass\" ></body>";
John Hartsock
That last example does not work.
Pointy
sorry your right it should be \" not ""
John Hartsock
+3  A: 

In Javascript a string is a sequence of zero or more Unicode characters enclosed within single or double quotes (' or "). Double-quote characters may be contained within strings delimited by single-quote characters, and single-quote characters may be contained within strings delimited by double quotes.

In client-side JavaScript programming, JavaScript code often contains strings of HTML code, and HTML code often contains strings of JavaScript code. Like JavaScript, HTML uses either single or double quotes to delimit its strings. Thus, when combining JavaScript and HTML, it is a good idea to use one style of quotes for JavaScript and the other style for HTML.

ungarida