Using ; after for loop changes the output, why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Using ; after for loop changes the output, why?

var x=1; for(var i=0; i<6; i++); { x+=i; } console.log(x); Output: 7 Otherwise the output is 16

3rd Jan 2019, 8:40 PM
Syeda Zobia Kanwal
Syeda Zobia Kanwal - avatar
3 Answers
+ 1
Because when you use semicolon the content in the curly brackets is skipped => the loop runs, when i becomes 6 then the loop ends. The value of i which is printed is 6 + 1 = 7.
3rd Jan 2019, 8:55 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
Cause x+=i Is not part of for loop
3rd Jan 2019, 8:48 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar
0
var x=1; for(var i=0;i<6;i++);//here ends for loop last value of i is 6 x+=i;//x+=6. x=7
3rd Jan 2019, 8:52 PM
Cлaвeн Ђервида
Cлaвeн Ђервида - avatar