66

Possible Duplicate:
When to Use Double or Single Quotes in JavaScript

This is specifically for jQuery not JavaScript in general since I'm not a JavaScript programmer and I'm starting to learn jQuery.

With the release of jQuery 1.6 was there a specific change on using single or double quotes?

For example, would the following function work in jQuery 1.6?:

$('.selector').show();

Or do I have to use double quotes?:

$(".selector").show();

Thanks in advance for any clarification.

Community
  • 1
  • 1
Ricardo Zea
  • 10,053
  • 13
  • 76
  • 79
  • 1
    related to http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript – MatTheCat Aug 11 '11 at 14:52
  • Ah! This is nice, thanks for the link. – Ricardo Zea Aug 11 '11 at 15:01
  • 4
    Considering this question is specifically scoped to jQuery, and the question identified as having already answered it deals broadly with native JS, and said question doesn't have a jQuery-relevant answer, this question should not be marked as a duplicate. – gfullam Dec 03 '14 at 20:47
  • 1
    I think except jQuery guidelines and special usage for clarifying scripts, One useful point (at least for me) may better dealing with nested strings, e.g. using server side expression inside jS expression: var j='@helper.func("value")' etc. – QMaster Mar 01 '18 at 01:30

3 Answers3

43

JQuery Core Style Guidelines says:

"Strings should always use double-quotes instead of single-quotes."

rdok
  • 413
  • 1
  • 6
  • 14
Frink
  • 478
  • 5
  • 2
  • Well, according to jQuery's own guidelines, this is right answer. However, something interesting to consider: With [this test](http://jsfiddle.net/5HhWF/1/) I get faster response with single quotes in Firefox 17.x (at the moment of typing this). But in Chrome, although different, the difference is really small... so small I consider it no-difference in using single vs. double quotes. Taken from [this comment](http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript#comment4970495_242833) – Ricardo Zea Nov 27 '12 at 03:40
  • The importance of using double quotes is apparently relevant to execution. When I run `$("#target h2" ).replaceWith('

    I'm h3

    ')` (with single quotes) I get a syntaxerror but `$("#target h2" ).replaceWith("

    I'm h3

    ")` (with double quotes) it executes as expected!
    – Bentley4 Sep 30 '13 at 17:00
  • 5
    Of course you get an error because you terminate your string with the single quote in "I'm". You have to escape the single quote in your string by a leading backslash and it works. `$('#target h2' ).replaceWith('

    I\'m h3

    ')`
    – sannoble Jan 07 '14 at 11:34
  • @Bentley4 Agreed. I was similarly given an error when attempting to run `$("#flower-description").load('fragments/lilies.html');` Do you know where in the jquery documentation it say's this syntax is not allowed? – Daniel Dropik Feb 14 '14 at 11:30
  • 16
    How can this be an accepted answer? That sentence is a **STYLE** guide for JQuery Foundation "internal" coding style, that means projects that include JQuery itself and related libraries. It is not meant to impose a style on JQuery users and it is not a programming language restraint. In fact this is not even an answer to the questions asked. `would the following function work in jQuery 1.6?:` **YES** `Or do I have to use double quotes?` **NO, but you could**. – FrancescoMM May 06 '14 at 07:37
  • @Frink I could not find that quote anywhere in that link. A Google search for that quote got me this SO post and [this blog](http://www.sitepoint.com/jquery-syntax-guidelines/), but I cannot find that quote anywhere in the JQuery documentation/guidelines. Could that statement have been removed? – modulitos Oct 04 '14 at 19:39
  • @FrancescoMM I assume you use single quotes then? – NorCalKnockOut Sep 09 '16 at 21:08
  • 1
    @NorCalKnockOut I usually use double quotes, but if you like single quotes, you can use them. You choose, that is the point. They are *the same*. – FrancescoMM Sep 11 '16 at 07:34
19

You are allowed to use both. In JavaScript there is no difference between both (while e.g. in PHP there is a difference).

By the way, this is a JavaScript question. You are passing a string to the $() function, and strings have to be surrounded by ' or ".

js-coder
  • 8,134
  • 9
  • 42
  • 59
8

jQuery is JavaScript and as such emits the same behavior: Both behave the same, because both represent a string.

Is there any specific reason you think that jQuery 1.6 changed something here?

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • Yeah, we have this slideshow that's not working anymore, a developer changed the jQuery version call from 1.3.2 to 1.6 during troubleshooting and since I don't have access to the server, my only resource was asking. The script that handles the slideshow is using single quotes so I thought something had changed in 1.6. Nonetheless, your explanation is very helpful, at least for JavaScript n00b like me :) – Ricardo Zea Aug 11 '11 at 15:00