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