2

I heard somewhere that its better to write this:

$(document).ready(function() {
    $('a').append('<strong>Hello</strong>');
});

instead of this:

$(document).ready(function() {
    $("a").append("<strong>Hello</strong>");
});

But the last one, is the way that its written in the official documentation of jQuery.

So which gives more problems when the code gets larger? single (') or double (") quotation? or its irrelevant?

alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • 1
    It's irrelevant, see http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript for suggestions. – Michael Greene Jan 02 '10 at 15:24

2 Answers2

3

In javascript, it is irrelevant. Both mean the same thing.

You may have heard the advice form languages like Perl where "" means interpolated string and '' means string literal. In which case '' incurs less processing overhead.

In javascript there are two ways to quote strings to allow simple shallow nesting of quotes:

div.innerHTML = '<a href="www.google.com">google</a>';
div.innerHTML = "<a href='www.google.com'>google</a>";
slebetman
  • 109,858
  • 19
  • 140
  • 171
  • I tend to use single quotes for JavaScript for that reason: though both are equally acceptable, double-quotes are by far the more usual delimiter for HTML/XML. – bobince Jan 02 '10 at 15:49
0

The only real advantage that I can see to using double quotes as a standard is if your strings contain apostrophes (single quotes), you don't have to change your style to accommodate them. Otherwise, javascript allows quoting with either single or double quotes.

Having said that, I tend to use single quotes for javascript out of a habit picked up long ago. Unfortunately, I'm not even that consistent about it.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795