Forums

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?

#194477
shaneisme
Participant

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;
}

console.log(zing());

This will show “bar” in the console.

But this:

function foo() {
  var bar = 3;
}

function zing() {
  return bar;
}

console.log(zing());

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

More: http://www.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/