5

for example here in documentation http://api.jquery.com/attribute-contains-selector/ it says

$('input[name*="man"]')

I would write instead

$("input[name*='man']")

is there any reason to use single or double quotation marks on inside or outside ?? is it just a matter of taste?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
IAdapter
  • 62,595
  • 73
  • 179
  • 242
  • 2
    Duplicate of http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript – Shai Mishali Jan 15 '12 at 09:12
  • 1
    @ShaiMishali: Not a duplicate, as this also involves the syntax for jQuery selectors. – Guffa Jan 15 '12 at 09:39
  • @Guffa I don't really agree. jQuery is just a Javascript library and the "quotes" thing is the same for both. – Shai Mishali Jan 15 '12 at 14:52
  • 2
    @ShaiMishali: Yes, jQuery is not a separate language, but the selectors used by jQuery is. It's not Javascript that is used in the selectors, but a syntax based on CSS selectors. The fact that Javascript syntax and selector syntax uses the same quotation marks has nothing to do with the fact that jQuery is a Javascript library. – Guffa Jan 15 '12 at 14:59

2 Answers2

5

It's just a matter of taste, and sometimes convenience.

You are using quotation marks both in Javascript and in a jQuery selector, and in both cases you can use either apostrophes (') or quotation marks (") to delimit strings.

In some cases it's more convenient to use one type over the other, for exampe when you have Javascript code in an HTML attribute, as apostropes doesn't need to be escaped:

<div onclick="alert('Hello world!');">

compared to:

<div onclick='alert(&quot;Hello world!&quot;)'>
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • can you use a mix of apostrophes and strings? I am trying to debug a javascript problem. Essentially, can you do alert('Hello World") – bernie2436 Mar 12 '12 at 16:22
  • 1
    @akh2103: No, the starting end ending delimiter have to match. Inside a string delimited by one, the other is just a regular character, as in: `alert("It's a test.");` – Guffa Mar 12 '12 at 16:32
2

Yes, it's a matter of taste. I generally use double quotes when the string literal contains single quotes, and vice-versa, to avoid having to escape them.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255