View Full Version : single and double quotes

04-23-2004, 05:32 PM
every reference to single and double quotes in js states that they're either interchangeable or redundant; in praxis i've found this isn't true at all. can't seem to find a simple answer anywhere, and despite several hundred experiments i'm unable to find a pattern.

any insight?


Roy Sinclair
04-23-2004, 05:39 PM
They must also match, you can't open a quoted passage with a double-quote and end it with a single quote.

If you still don't understand show some examples of what you're having trouble with.

04-25-2004, 08:37 AM
yeah i think i understood the match part - always in pairs, first in, last out (if i'm wrong lemme know) - and it's not actually creating a problem, but i'll put a couple examples of inconsistencies i've noticed...

on some functions the arguements have single quotes


on others they won't work if single quotes are included, and prefer to be bare


and using the setTimeout method, if used in the verbiage of the function included in the external script or in the head, it likes double quotes

function example(w, x, y, z){
if (!document.all)
if (w!= "[w]"){

but if i use that same thing inline (calling the function with setTimeout), it fails:


nor does






but.... sometimes this will...


i'm assuming sometimes has to do with the quote use/syntax of the function being called, but i can't find a pattern so far...

Philip M
04-25-2004, 08:47 AM
I have never read of a difference between single and double quotes in JavaScript, but there is a difference in Perl.

What browser are you using?

Is it anything to do with putting a semi-colon ; inside the quotes? Surely the ; should be outside, thus:-


04-25-2004, 08:52 AM
i dont' think so... (but then again i'm not very good at this)

the quotes are to end what happens onmouseover, the semi-colon is to separate the functions that occur onmouseover...


as far as i knew, the semi-colon ended a line and the function is still within the line param of the event...

im getting more confused :|

What browser are you using?
and i'm on xp using ie6,nn7.1,moz5.0

04-25-2004, 09:04 AM
Your examples are different.



passes a object-reference; but this:


passes a string.

Just like this:


is a boolean argument; while this:


is the word "true".

Quotes are interchangeable, but generally only for strings.

There are exceptions though, such as key values in an object-literal - either of these is okay:

var myObj = { foo : 'bar' };
var myObj = { 'foo' : 'bar' };

04-25-2004, 09:08 AM
Your examples are different; this:


Passes a object-reference; but this:


Passes a string.
Quotes are interchangeable, but only for strings.

ok. i assume i understand object reference (... referencing an object, the target arguement of the function, the id of the element being affected)... and string means ... text only? no tags?

04-25-2004, 09:14 AM
Maybe a primer on data types would help - have a look at this http://javascript.about.com/library/weekly/aa040102a.htm

04-25-2004, 12:31 PM

thanks for the link. but... knowing that, and referencing your assertion that single quotes only return a string (text) - what's going on here?: (this works, and don't work if the ommission/commission of the quotes is altered on the mouseover event for any of the 3 functions it calls).

var timer=null;
px = (document.getElementById) ? "px" : "";
var offset = -50;
var distance = 200
var timerspeed =20;
var steps = 40;
var decrease = 0.09;
function getPos(id) {
if (document.getElementById) {
endPos = document.getElementById(id).offsetLeft;
else if (document.layers) {
endPos = document.layers[id].left;
function slide(obj) {
if (document.getElementById) {
el = document.getElementById(obj).style ;
if (document.layers) {
el = document.layers[obj] ;
el.xpos = parseInt(el.left);
if (el.xpos < endPos) {
else if (el.xpos >= endPos) {
distance = endPos - el.xpos + offset;
steps = distance*decrease;
el.xpos += steps;
el.left = el.xpos+px ;
timer1= setTimeout("slide('" + obj + "')",timerspeed);
function grow(obj) {
if (distance < 500)
obj.style.width = distance++;
setTimeout(function(){grow(obj)}, 10);
<a id="link" href="#" onmouseover="getPos('link');slide('layer');grow(layer);" style="position:absolute;top:0px;left:0px;width:100px;height:100px;background-color:navy;"></a>
<div id="layer" style="position:absolute;left:100px;top:0px;width:100px;height:100px;background-color:silver;"></div>

why is that? does it have to do with the id reference versus the ojb reference? (the motive function and the function to find position coords use getElementbyId(obj), while the resize function uses only (obj)...)

thanks cake for the time, sorry if im being a bother...

* (thanks and credit to glenngv for helping (basically re-writing) the resize function used in this example).

edit: forgot to close code tag

04-25-2004, 06:28 PM
You're confusing yourself by using the keyword "obj" for something which is not always an object.

Your slide function has a string:

el = document.getElementById(obj).style ;

The keyword "obj" refers to a string, which is the ID of the element. But in the grow function you use an object reference directly:

obj.style.width = distance++;

See the difference? It would be less confusing if you use a keyword that made sense for what it is, like "id" that you use for the getPos function

04-26-2004, 05:01 AM
i finally get it. thx.

04-26-2004, 05:07 AM
IE's global scoping of ids in elements added to your confusion.

<a id="link" href="#" onmouseover="getPos('link');slide('layer');grow(layer);" ...></a>
<div id="layer" ...></div>

In IE, all ids are considered global variables. So the div with id="layer" can be referenced by the global variable layer. You should avoid that bad practice of referencing elements only by their id. Only IE allows that. :rolleyes:
The proper way to reference element via its id is:


where layer is the id of the element.
You can use single or double quotes to surround the string literal layer. The quotes are not needed IF you store the id in a variable.

var theId = 'layer';

This is similar to what you did in the slide() function. You only used a confusing variable name for the function parameter which brothercake earlier pointed out.

Hope this enlightens you. :)