+ 1
why this for loop output 3 with global variable
explain me this code why for loop with global variable make output 3. if variable are put inside for loop or hoist above the undefined it gives 9 var i = 0,j = 0, count = 0; for(; i < 3; i++){ for(; j < 3; j++){ count++; } } console.log(count)
1 Resposta
0
It's because the second loop is executed only once instead of three times. When the second loop start for the first time, it will loop and incrament j until it has a value of 3. The j variable is a global variable, therefore it will stay there until the program ended. After the first loop, the loop will see that j variable has the value of 3, therefore it won't start again