What is the output of this code? for(var x =0; x==x; x++) { if(x > 5 && x < 8) { document.write(x); break;} } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What is the output of this code? for(var x =0; x==x; x++) { if(x > 5 && x < 8) { document.write(x); break;} }

3rd Mar 2019, 12:38 PM
david chongo
david chongo - avatar
5 Answers
+ 3
It's a challenge question, I wanted to find out how the answer actually came to 6.
3rd Mar 2019, 12:48 PM
david chongo
david chongo - avatar
+ 3
I have seen the answer. I just need to know the 'how' part. Regards
3rd Mar 2019, 12:48 PM
david chongo
david chongo - avatar
+ 3
Makes loads of sense!! Great thanks there!
3rd Mar 2019, 12:55 PM
david chongo
david chongo - avatar
+ 2
You can run it in your code playground
3rd Mar 2019, 12:45 PM
Seniru
Seniru - avatar
+ 1
david chongo lol i thought you were lazy to run the code. Then the explanation: This is basically and infinite loop (see the 2nd statement. It is always true. So the loop will continue without breaking). While it was running it increases x by 1. And inside the loop there is an if condition which checks whether x is in the range of 6 and 7. During.the loop x eventually become 6. So the statements inside the loop start to work. It 1st write 6 inside the body and then break the infinte loop. Hope you get it.
3rd Mar 2019, 12:52 PM
Seniru
Seniru - avatar