Jump to content

quotes and single quotes


etsted

Recommended Posts

i am trying a script that wont work when i use double quotes like this:

setTimeout('resetScroller("+el+")',speed);

 

but instead i have to use thi syntax:

setTimeout('resetScroller(''+el+'')',speed);

 

whats the diffrence? Because they wont give the same result.

Link to comment
Share on other sites

In your first example "el" is being interpretted as part of the string because you haven't closed the string:

In the second example you have quotation marks inside the string but you're closing the string before showing the variable. It doesn't matter whether they're single or double quotes.

 

All the following options are valid:

"resetScroller('" + el + "')""resetScroller("" + el + "")"'resetScroller('' + el + '')''resetScroller("' + el + '")'

The backslash is used to put a quotation mark inside a string that's delimited by the same quotation mark.

Link to comment
Share on other sites

The verb delimit: To fix or mark the limits or boundaries of; demarcate:

A string is delimited by either single-quotes (apostrophe character) or double-quotes. There is one delimiter preceding the string, and another delimiter following the string. If you want to use a single-quote inside a string that is delimited by single-quotes you need to use a backslash.

 

Look at how the syntax highlighter reacts to various strings based on what characters they contain:

var str;str = "It's called a "delimiter".";str = 'It's called a "delimiter".';  //'str = "It's called a "delimiter".";str = 'It's called a "delimiter".';

Green means that the character is part of the string, black means it's outside of the string.

Link to comment
Share on other sites

What is the best approach? I don't seem to do that too often in JS. Where would I need to do that?

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>passing funct args</title><script>var p = {};p.cnt = 0;p.delay = 1000;p.multi = 10;window.onload = init;function init() {getele(1).innerHTML = 'Initialized to a delay of '+ mult(p.delay,p.multi) +' milliseconds<br/>';setTimeout(function(){displaycnt(getele(1),getpcount());},mult(p.delay,p.multi));}function getpcount(){p.cnt = p.cnt+1;return p.cnt;}function getele(n){var str = 'out'+ n;return document.getElementById(str);}function mult(x,y){return x*y;}function displaycnt(ele,cnt){var d = new Date();ele.innerHTML += 'Count: '+ cnt +' at time: '+ d.toLocaleTimeString() +'<br/>';setTimeout(function(){displaycnt(ele,getpcount());},mult(p.delay,p.multi));}</script></head><body><h1>FUNCTION ARGUMENTS ARE SCREWY AS ######</h1><div id="out1"></div></body>    </html>
Link to comment
Share on other sites

It was intended to be a silly example that passes a lot of functions as arguments -- which was (my interpretation) the OP's core problem.

 

When exactly do you need to do what the OP is trying to do? Does that even work?

setTimeout('resetScroller('+el+')',speed);

...doesn't this work better?

setTimeout(function(){resetScroller(el)},speed);
Link to comment
Share on other sites

  • 2 weeks later...

yes, because the best practice way to use setTimeout / Interval is by passing a reference to a function

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...