Do while loop


do while

I don't understand why when I run the code on a random webpage(HTML and linked the js file and used breakpoints) in the console it prints out twice the messege. I mean this is how I think it should work:
The program read's the value(true) it goes in to the "do while" function, it print's out the console.log, then the value changes to false and when checked in the condition if it is still true(which is not) it should stop but it dosen't, it leaves the do function, takes the value(true) and goes one more time so it print's twice the "console.log". My question is why the program leaves the do while function?Why dosen't it stop's when the value turn's false?
PS: If I run this code in your compiler, it only print's out once, so what's the difference?
PS: Even this code is printed out twice, it goes twice, why?
var youHit =Math.random()

Replace this line with your code. 
var value = true;
console.log("What's up man");
value = false;


This loop will never end called infinite loop.This will crash your browser.

value = "true"; // wrong

because here written like this true is a string.but true/ false Boolean data type. so write without quotes.

value = "false"; // same here


still didan't anwered my question


It is difficult for me to understand your question, but it console.log-s twice because

This loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:


var youHit =Math.random()

if this code, as well as the other one, prints twice. Then it has nothing to do with your code but with your setup. What kind of IDE do you use? Which browser do you use to display it. What do your HTML and JS file look like aso.


I use Chrome and as a writeing program I use notepad++ if it's counts. No ideea what to tell you about IDE, and yes it print's out twice.


IDE is an integrated development environment:

It's fine if you don't use one. Have you tried it in a different browser as well and does it work? Could you post the code that you're using and what do you use to see the output regular chrome web dev tools or special plugins?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.