function f1() {
for (i = 0; i <= 5; i++)
console.log(i);
}
function foo() {
for (i = 0; i < 5; i++)
f1();
}
foo();
Hi, I'm trying to understand why the result of executing foo is:
0
1
2
3
4
5
And not:
0
1
2
3
4
5
0
1
2
3
4
5
0
1
2
3
4
5
0
1
2
3
4
5
0
1
2
3
4
5
It's a slide I am reading about JS and its talking about when you don't use var then it is defined on the global object and provides this example without any further details why we get the result.
I thought it will simply loop and run the f1 function each time until its less than 5.
Please help me understand.
Thanks
iis global and shared between your functions. Usevarto make both instances local.iis a shared global variable, sofoostarts, sets is to0then callsf1which loop on it up to5, when control returns tofooit founds thatiis5and so it ends the loop.