LLLLLLL Posted March 19, 2012 Share Posted March 19, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/259286-variables-from-first-function-affected-by-same-name-in-second-function/ Share on other sites More sharing options...
LLLLLLL Posted March 19, 2012 Author Share Posted March 19, 2012 http://stackoverflow.com/questions/6038958/javascript-for-loop-index-variables-become-part-of-global-scope I guess that is the answer. Sigh. Quote Link to comment https://forums.phpfreaks.com/topic/259286-variables-from-first-function-affected-by-same-name-in-second-function/#findComment-1329171 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.