Search results
Learn how to use the while loop and the do while loop in JavaScript to execute a block of code repeatedly until a condition is true. See syntax, examples, and compare with the for loop.
- While
The while statement creates a loop (araund a code block)...
- While
- Overview
- Syntax
- Examples
- Browser compatibility
The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.
condition
An expression evaluated before each pass through the loop. If this condition evaluates to true, statement is executed. When condition evaluates to false, execution continues with the statement after the while loop.
statement
An optional statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ /* ... */ }) to group those statements.
Using while
The following while loop iterates as long as n is less than three. Each iteration, the loop increments n and adds it to x. Therefore, x and n take on the following values: •After the first pass: n = 1 and x = 1 •After the second pass: n = 2 and x = 3 •After the third pass: n = 3 and x = 6
Using an assignment as a condition
In some cases, it can make sense to use an assignment as a condition. This comes with readability tradeoffs, so there are certain stylistic recommendations that would make the pattern more obvious for everyone. Consider the following example, which iterates over a document's comments, logging them to the console. That's not completely a good-practice example, due to the following line specifically: The effect of that line is fine — in that, each time a comment node is found: 1.iterator.nextNode() returns that comment node, which gets assigned to currentNode. 2.The value of currentNode = iterator.nextNode() is therefore truthy. 3.So the console.log() call executes and the loop continues. …and then, when there are no more comment nodes in the document: 1.iterator.nextNode() returns null. 2.The value of currentNode = iterator.nextNode() is therefore also null, which is falsy. 3.So the loop ends.
BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.
The JavaScript while and do…while loops repeatedly execute a block of code as long as a specified condition is true. In this tutorial, you will learn about the JavaScript while and do…while loops with examples.
Learn how to use the while statement to create a loop that executes a block as long as a condition is true. See syntax, flowchart, and code examples of the while loop in JavaScript.
Learn how to use the while loop in JavaScript to execute a code block as long as a condition is true. See examples, syntax, browser support and related statements.
Learn how to use while loop, a control flow statement, in JavaScript with examples and diagrams. See how to use nested while loop, variable in while loop, do while loop, and difference between for and while loop.
People also ask
What is a DO WHILE loop in JavaScript?
What are some examples of while loop in JavaScript?
How does a while loop work?
What is a loop in JavaScript?
Sep 12, 2023 · while (condition) statement. If the condition becomes false , statement within the loop stops executing and control passes to the statement following the loop. The condition test occurs before statement in the loop is executed. If the condition returns true, statement is executed and the condition is tested again.