03-06-2012, 09:11 PM
Can you please advise on this.
I tried to research this and everyone contracdicts each other?
03-06-2012, 09:28 PM
To avoid escaping of inner quotes you might consider using different quotes inside a string than the string delimiter quotes, but this is still up to you. Feel free to escape quotes whereever you want to :-)
All of this is equally possible
var mystring1 = "This is my string";
var mystring2 = 'This is my string';
var mystring3 = "This is my 'string'";
var mystring4 = 'This is my "string"';
var mystring5 = "This is my \"string\"";
var mystring6 = 'This is my \'string\'';
var mystring7 = "This is my \'string\'";
var mystring8 = 'This is my \"string\"';
03-06-2012, 10:46 PM
I like to use single quotes.
Reason: I find that within a string the " char is used far more often than the ' char, and switching to single quotes from double some years ago has greatly reduced my need for escaping \'. Also, I like to use .innerHTML to inject HTML in certain cases, and since I always use double quotes in declaring tag properties I never have to worry about mixing the two.
I think I agree that I have never heard either to be "best practice", and that you should do what makes your code easier for you to understand, and manage.
03-07-2012, 02:07 AM
Use whichever one will reduce or eliminate the number of quotes in the string that need to be escaped.
That may mean that with two adjacent assignments that you use one around the first and the other around the second.
You can even completely avoid the need to escape them by swapping mid string eg. ' " ' + " ' " is a single string containing space, double quote, space, space, single quote, space