0

Im relatively new to Javascript and as such trying to understand what it is I am writing.

I get the while loop eg:

var i = 0;
while (i < 10) {
 console.log('i is less than 10');
 i++;
}

but what i dont understand is when do you use

while(false) {
document.write('do this');
}
document.write('do that');

the "false" value confuses me because i don't understand what i am checking against

2
  • 4
    "when do you use ..." - you don't. Commented May 8, 2015 at 4:05
  • "i don't understand what i am checking against" The loop body is executed as long as the condition evaluates to false. The false literal (obviously?) evaluates to false. Commented May 8, 2015 at 4:15

3 Answers 3

5

It will never run. False is never true, so anything in while(false) {...} will not be executed.

You would not use this in practice. It effectively disables a block of code.

You asked what it is "checking against." It's checking to see if false evaluates to true. It doesn't, so it doesn't run anything in the while block.

Sign up to request clarification or add additional context in comments.

2 Comments

so would you ever type while(false) ?
No. It's useless, unless you're trying to disable something temporarily for testing. But there's no reason not to just comment out the code in question, instead -- comments are easier to read.
2

while(false) is a useless code because it won't output any or do any at all. The while() loop will always check for boolean conditions and that means that the statement inside it must return true or false. True and False are valid arguments inside the while loop. while(false) will not output any and while(true) will run infinitely.

Arguments inside the while() loop will always evaluate to true or false. Therefore if you have var num = 5; and while(num==5) then (num==5) will be evaluated to true and if the condition is not met then it will be false instead.

5 Comments

yes i get this part but what i don't understand is what is it checking against to determine if it is true or false?
@Gavib: The false literal (obviously?) evaluates to the value false (not true). Think about like this: while(x) is the same as while(Boolean(x) === true). So while(false) is the same while(Boolean(false) === true) which is false, because false === true is false.
@GavinWood false is the final value. You didn't check it against any. Say you have var num=5; therefore: while(num==5) is also while(true) and it will always equate to that.
ok so case and point i will never write while(false) or while(true)? even if a variable is set to a certain value?
No good programmer will ever write while(false) and while(true). Always set a condition that can be met either true or false and make sure that it will not loop infinitely.
0

It depends on what you want to test. You didn't really include that in your second code example. As long as you have a variable (or you want to see if it exists), you can do this, though.

If you want to loop while a variable is undefined, you can do it like this:

while(i == undefined) {
runCode()
}

if you want to run while a condition is false, (for instance, i = 10), you'd have to make it loop while a condition is true that would make the condition you're testing for false, like this:

while(i != 10) {
runCode()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.