0

I ve been learning JQuery, and everything was clear. Until I reached the strings. So far I knew that when you want to call something you do it this way $('callSomething'). Now Im learning how to move html elements around, and it was written to call something with double comment lines $("callSomething").

I started playing around the code, and I ve noticed that no matter what I will use ' ' or " " the code will work.

     $('#one').after("<p>test</p>");
    var $p = $("p");
    $("#two").after($p)

Now I'm not sure what to use where. I understand that " " is used to call Strings. But I could use it to call elements too.

My question: Is there a specific reason when to use ' '? Or should I always use " " since its working and save myself the confusion?

Any explanation maybe on when to use ' ' and when '" "(if there is actual difference between them other than calling a string("< .p>test<./ p >")`?

Thank you for your time

  • Hope this can help http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript – Kishan Rajdev May 06 '15 at 08:01
  • This is about JavaScript syntax. It's not specific to jQuery or any other JavaScript library. – Kos May 06 '15 at 08:01
  • It is not a duplicate question. The other question is asking about how Quotes work in strings. My question is about Selecting Elements from html. I will edit the question – Metalbreath May 06 '15 at 16:06

4 Answers4

1

This can help,

I wouldn't say there is a preferred method, you can use either. However If you are using one form of quote in the string, you might want to use the other as the literal.

alert('Say "Hello"');
alert("Say 'Hello'");

The most likely reason is programmer preference / API consistency.

When to use double or single quotes in JavaScript?

Community
  • 1
  • 1
Kishan Rajdev
  • 1,919
  • 3
  • 18
  • 24
  • Copying information from another answer and then linking to that answer is usually not a good idea. Instead you should comment that the question is a duplicate (until you have enough rep to close vote). – Davin Tryon May 06 '15 at 08:05
0

There is no difference between the two versions of strings except this one:

Imagine you want to use a quotation mark inside a string:

"This is just a lil' test"

Or:

'"give them hell", he said'

You don't have to escape quotation marks in those two cases.

Peanut
  • 3,753
  • 3
  • 31
  • 45
  • So $('div).fadeOut() and $("div").fadeOut() are exactly the same... Thank you very much :) I was worried that I was doing something wrong. It seems though that many JQuery programmers prefer the single Quote when they select an element from html. – Metalbreath May 06 '15 at 16:04
0

Both are equivalent, it's mainly a matter of preference and code legibility. If your string contains a single quote, you'll probably want to enclose it in double quotes and vice versa, so you don't have to escape your quotes, e.g.

"It's nice" vs 'It\'s nice'

or

'And she said "Oh my God"' vs "And she said \"Oh my God\""

You can find a more exhaustive answer to that question at https://stackoverflow.com/a/242833/4604579

Community
  • 1
  • 1
Val
  • 207,596
  • 13
  • 358
  • 360
-2

in javascript there is no difference. in other languages like c, ' will be used for characters and " will be used for character arrays aka strings.

GottZ
  • 4,824
  • 1
  • 36
  • 46
  • There is a difference when using single/double quotes inside other single/double quotes. – Davin Tryon May 06 '15 at 08:01
  • 1
    thats solved by escaping. in javascript there is NO difference between " and ' except you need to follow the rules of escaping ' inside ' and " inside ".. wich is obvious. – GottZ May 06 '15 at 08:04