DEV Community

Cover image for JS Quiz
Saeid
Saeid

Posted on

JS Quiz

What is the output of this code?
with explanation (comment your answers)
var sum=0;
for(i=4; i<8; i++) {
if (i == 6) {
continue;
}
sum += i;
}
document.write(sum);

Top comments (1)

Collapse
 
norrova profile image
Norro valentin

Firstly we define a variable to 0.
We create a loop who start to 4 and stop to 7 because (i<8).
When i equal to 6 we continue to the next iteration i equal to 7.

Result :

  1. sum = 4
  2. sum = 9 (4+5)
  3. continue => (sum = 9)
  4. sum = 16 (9+7)

the page display 16.

note : In the loop, post-increment is unnecessary, use pre-increment