Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Single ' or double " quotes?

Reply
Thread Tools

Single ' or double " quotes?

 
 
DrKen
Guest
Posts: n/a
 
      06-15-2011
In looking at various JavaScript examples, I see a lot of
inconcsistency over when to use ' ' and when to use " ". For
example, should I code
var user = document.getElementById('userid');
or
var user = document.getElementById("userid");

Is there a rule I can follow to decide which kind of quote to use?
I'm used to languages that are very very picky about this. Thanks.

Ken
 
Reply With Quote
 
 
 
 
Gregor Kofler
Guest
Posts: n/a
 
      06-15-2011
Am 2011-06-15 20:45, DrKen meinte:
> In looking at various JavaScript examples, I see a lot of
> inconcsistency over when to use ' ' and when to use " ". For
> example, should I code
> var user = document.getElementById('userid');
> or
> var user = document.getElementById("userid");


It's completely irrelevant.

> Is there a rule I can follow to decide which kind of quote to use?


Whatever you prefer. I prefer double quotes. Though I can't really say
why...

> I'm used to languages that are very very picky about this. Thanks.


What do you mean by "picky"? There are differences for example with PHP
(interpolation with double quotes), but PHP is not really "picky" about
that - it won't shout when using double quotes when not necessary.


Gregor


--
http://vxweb.net

 
Reply With Quote
 
 
 
 
Jukka K. Korpela
Guest
Posts: n/a
 
      06-15-2011
2011-06-15 22:54, Gregor Kofler wrote:

>> var user = document.getElementById('userid');
>> or
>> var user = document.getElementById("userid");

>
> It's completely irrelevant.


Taken in isolation, yes.

Single quotes are however convenient in client-side JavaScript, when
your code might be embedded into event attributes, conventionally
delimited by double quotes.

--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      06-15-2011
Jukka K. Korpela wrote on 15 jun 2011 in comp.lang.javascript:

> 2011-06-15 22:54, Gregor Kofler wrote:
>
>>> var user = document.getElementById('userid');
>>> or
>>> var user = document.getElementById("userid");

>>
>> It's completely irrelevant.

>
> Taken in isolation, yes.
>
> Single quotes are however convenient in client-side JavaScript, when
> your code might be embedded into event attributes, conventionally
> delimited by double quotes.


And also in serverside javascript,
when building html strings
or complex structures.

sql strings also need single quotes.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      06-16-2011
In comp.lang.javascript message <a64fb8c6-7a68-4405-a8e8-65b3fd22bb65@j1
3g2000pro.googlegroups.com>, Wed, 15 Jun 2011 11:45:50, DrKen
<> posted:

>In looking at various JavaScript examples, I see a lot of
>inconcsistency over when to use ' ' and when to use " ". For
>example, should I code
>var user = document.getElementById('userid');
>or
>var user = document.getElementById("userid");
>
>Is there a rule I can follow to decide which kind of quote to use?
>I'm used to languages that are very very picky about this. Thanks.


I don't see that anyone has actually said that the closing quote must be
literally the same as the opening one.

If you want to put a ' in the string, it's easier to use " quotes.
If you want to put a " in the string, it's easier to use ' quotes.

If you want to put the string in something that is already in " quotes,
use ' quotes, and /vice versa/.

If you want to put a " in a string already in " quotes, use \".
If you want to put a ' in a string already in ' quotes, use \'.

In a string, instead of " you can write \x22 or \u0022.
In a string, instead of ' you can write \x27 or \u0027.

If you do not want to use \, then you can write something like
'aaa"bbb' + "ccc'ddd".

Also, read (its free) ECMA 262 version 5 section 7.8.4.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      06-17-2011
2011-06-16 21:56, Dr J R Stockton wrote:

> I don't see that anyone has actually said that the closing quote must be
> literally the same as the opening one.


I thought that was pretty obvious if you think about the very idea of
string literal delimiters.

> If you want to put a ' in the string, it's easier to use " quotes.
> If you want to put a " in the string, it's easier to use ' quotes.


Marginally easier. This hardly justifies mixed style, i.e. using "
quotes for some literals and ' quotes for some. I tend to be sloppy and
use quotes by my mood, but I shouldn't.

It’s best to stick to one delimiter, normally ' quotes (for a few
reasons, none of which is compelling but sufficient for making a
choice). And in the rare cases where you need a quote inside the
literal, you simply escape it with “\”.

When the string literal contains an expression in a natural language, it
normally should not contain " or ' at all, as they are “computer
quotes”, not correct in any human language (though still widely used as
a holdover from the bad old times when typewriters and ASCII ruled the
Earth). When you use proper language-specific quotation marks, you can
write then inside a literal with no escaping, e.g.
'it’s easy'
or (if you haven’t learned how to type proper quotes)
'it\u2019s easy'

--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      06-18-2011
In comp.lang.javascript message <iteo0c$b6r$>, Fri, 17
Jun 2011 08:17:02, Jukka K. Korpela <> posted:

>2011-06-16 21:56, Dr J R Stockton wrote:
>
>> I don't see that anyone has actually said that the closing quote must be
>> literally the same as the opening one.

>
>I thought that was pretty obvious if you think about the very idea of
>string literal delimiters.


Never presume that the other fellow has the same idea of what is
obvious.

I recall being telephoned by an inexpert TurboPascal programmer in
another building, who said that he had an error and gave its number -
perhaps 203. I recognised that; he needed to put something like
{$M 4096, 0, 0}
as the first line. So I said to put a first line of 'open-twiddly-
brackets dollar Em space ... '. No good, compiler objected. We checked
verbally that he had put what I said, or so we thought. Only when I
remembered that, over the network, I could actually see the code did I
perceive that he'd parsed my words as 'open twiddly-brackets dollar Em
space ...' and so he had written open{$M 4096, 0, 0) or
similar.




>teral, you simply escape it with “\”.
>
>When the string literal contains an expression in a natural language,
>it normally should not contain " or ' at all, as they are “computer
>quotes”, not correct in any human language (though still widely used as
>a holdover from the bad old times when typewriters and ASCII ruled the
>Earth). When you use proper language-specific quotation marks, you can
>write then inside a literal with no escaping, e.g.
>'it’s easy'
>or (if you haven’t learned how to type proper quotes)
>'it\u2019s easy'


Life is too short and inter-cranial memory too cluttered to use fancy
quotes except when publishing professionally - and then one needs to be
sure that the user's font will always understand them.

<FAQENTRY> OTOH, it might be useful to have in the FAQ a small subset of
\x##, \u####, and &word; codings, with a link or two to a fuller list.
<http://en.wikipedia.org/wiki/List_of_Unicode_characters> is useful, and
more concise than what unicode.org has. And one can take a local copy
and excise the superfluous - I expect that few of us will foresee much
need to write in Linear B - though the \u28## block may some day become
needed.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      06-20-2011
2011-06-18 23:39, Dr J R Stockton wrote:

>>> I don't see that anyone has actually said that the closing quote must be
>>> literally the same as the opening one.

>>
>> I thought that was pretty obvious if you think about the very idea of
>> string literal delimiters.

>
> Never presume that the other fellow has the same idea of what is
> obvious.


That would mean ending all human communication. All efforts to
communicate must postulate a huge amount of assumptions (though partly
varying). I don' know why you felt it to be necessary to state the
obvious. It seems that you wanted to say that the delimiter cannot
appear in the delimited string.

> Life is too short and inter-cranial memory too cluttered to use fancy
> quotes except when publishing professionally


At least when you use JavaScript on or for web pages, you are using it
when publishing. Don't you think the quality should be professional?

It's the ASCII quotes that are "fancy", computerese. They were
introduced on mechanical typewriters, than transferred to early
character codes on computers (and keyboards), lumping together opening
and closing quotation marks and several other characters, even intending
the quotes play, via overprinting, dual roles as diacritic marks. There
is little reason to preserve that barbaric treatment of human language
punctuation.

As I mentioned, using proper punctuation marks _avoids_ trouble, since
in JavaScript, as in most computer languages, they are just character
data with no syntactic meaning.

> - and then one needs to be
> sure that the user's font will always understand them.


Do you expect to find many fonts that do not contain the quotation marks
as used in English?

> <FAQENTRY> OTOH, it might be useful to have in the FAQ a small subset of
> \x##, \u####, and&word; codings, with a link or two to a fuller list.


Hardly. Any subset would be arbitrary and biased. FAQs are no
replacement for references. And &word; codings aren't JavaScript at all.

> <http://en.wikipedia.org/wiki/List_of_Unicode_characters> is useful, and
> more concise than what unicode.org has.


And infinitely more unreliable. It's also far too long to give an
overview, and it's far to limited to give a complete list. Even the page
itself says: "This article may be too long to read and navigate
comfortably. Please consider splitting content into sub-articles and
using this article for a summary of the key points of the subject."
That's a polite way of saying that it is overlong, messy, and hasn't
much point in it.

--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      06-21-2011
In comp.lang.javascript message <itmnod$t2s$>, Mon, 20
Jun 2011 09:01:48, Jukka K. Korpela <> posted:

>2011-06-18 23:39, Dr J R Stockton wrote:
>


>At least when you use JavaScript on or for web pages, you are using it
>when publishing. Don't you think the quality should be professional?


No. That would in fact be deceptive, since I am not a professional
publisher. And nothing else about my site is professional either, since
professional work is checked before release to the public. I just, as
an amateur, do the best I can to get my meaning across and give the
right results.



>As I mentioned, using proper punctuation marks _avoids_ trouble, since
>in JavaScript, as in most computer languages, they are just character
>data with no syntactic meaning.
>
>> - and then one needs to be
>> sure that the user's font will always understand them.

>
>Do you expect to find many fonts that do not contain the quotation
>marks as used in English?


That's not the point. I do not expect that every font used will contain
the fancy quotes; I do not know, and I do not need to. And it's not
only a matter of fonts used by Web browsers; if I copy'n'paste source
here, I don't want readers to be interrupted by seeing coded characters.




>> <FAQENTRY> OTOH, it might be useful to have in the FAQ a small subset of
>> \x##, \u####, and&word; codings, with a link or two to a fuller list.

>
>Hardly. Any subset would be arbitrary and biased. FAQs are no
>replacement for references. And &word; codings aren't JavaScript at
>all.


An intelligent FAQ write will not be much biased; and, if he is, it does
not matter. Those who write JavaScript commonly write HTML and need
such as &nbsp; .. &emsp; for spacing in forms. Eschew pedantry.



--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      06-22-2011
2011-06-21 22:57, Dr J R Stockton wrote:

>>> - and then one needs to be
>>> sure that the user's font will always understand them.

>>
>> Do you expect to find many fonts that do not contain the quotation
>> marks as used in English?

>
> That's not the point.


So what _is_ your point then?

> I do not expect that every font used will contain
> the fancy quotes; I do not know, and I do not need to.


So if you don't know about it, why are you referring to font issues at
all? If I mentioned that the proper multiplication sign in
natural-language texts is "", not "*" as in programming languages,
would you also worry about fonts? Why aren't you worrying about the need
to be sure that the user's font will always understand the characters "
and '?

> And it's not
> only a matter of fonts used by Web browsers; if I copy'n'paste source
> here, I don't want readers to be interrupted by seeing coded characters.


Then don't copy and paste source here; copy and paste a URL instead.

> Those who write JavaScript commonly write HTML and need
> such as&nbsp; ..&emsp; for spacing in forms.


No they don't. HTML specifications explicitly tell them to use style
sheets and not space characters for layout. And &emsp; would be a
particularly bad choice if someone really used it - _it_ may create real
font problems.

Besides, if people who write JavaScript commonly do something else as
well, that doesn't imply it should be discussed in a JavaScript FAQ. For
example, the FAQ is no place for discussing eating, sleeping, walking,
or sex, even though many JavaScript programmers surely do some of these
and may have serious problems with them.

--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid on load; replace all double single quote to single quote to display to user Eric Layman ASP .Net 3 04-14-2007 07:16 AM
Custom Taglib problems - instead of a single instance per page, I have a single instance per application. chris brat Java 1 05-10-2006 11:16 AM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
BoundColumn single DataBind equiv but not in a single row? Randall Parker ASP .Net 1 12-12-2005 04:11 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57