Jump to content

Variables from first function affected by same name in second function


LLLLLLL

Recommended Posts

If something calls function f1 below, what do you expect to see in the alert?

 

	function f1()
	{
	  for ( i = 0; i < 8; i++ )
	  {
	     other_func();
	     alert( i );
	  }
	}

	function other_func()
	{
	  for ( i = 0; i < 100; i++ )
	  {
	  }
	}

 

The answer is 100, and we've found this to be quite odd. If either of the functions used "var i" instead of just "i" in the loop, you'd get 0, 1, 2,...

 

Why is this, exactly?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.