The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript If statment in slider function to loop to start? Reply To: If statment in slider function to loop to start?


One thing that might not be clear to you, that I just thought of, is scope.

You might be wondering, “Well, if the variables are declared in that function, why do we bother returning them?” The answer is scope. Inside a function, with a variable declared, those variables are not accessible outside the function. You can only get stuff that’s in the same scope or “above”.

For instance:

var foo = 'bar';

function zing() {
  return foo;


This will show “bar” in the console.

But this:

function foo() {
  var bar = 3;

function zing() {
  return bar;


This will return undefined because function zing() has no idea what the variable bar is.