Definition and Usage
The while statement consists of a condition and a code block.
It is a loop that repeatedly executes the code block while the condition is true.
The loop ends when the condition becomes false.
Additional Explanation
The while statement is a Pre-test loop that checks the condition before the code block is executed. More precisely, it checks the condition, and if the condition is met, the code block is executed.
The while statement is very useful when the number of iterations is not predetermined, allowing you to repeatedly execute a code block based on a Boolean condition.
Basic Example
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
// Output: 1 2 3 4 5
Syntax
The while statement consists of a condition enclosed in parentheses (()) and a code block ({}).
while (condition) {
// Code block to execute
}
condition
This is the condition expression that determines whether to execute the code block ({}). It is required.
- If this condition evaluates to
true, the code block ({}) is executed. - If this condition evaluates to
false, thewhilestatement is no longer executed and terminates. - If the condition expression is omitted, a syntax error occurs.
Execution Order of the while Statement
When the while statement is executed, it follows the sequence below.
- The
conditionis evaluated. If the condition evaluates totrue, the code block ({}) is executed. However, if it evaluates tofalse, thewhilestatement is no longer executed and terminates. - If the
conditionevaluates totrue, the code block ({}) is executed. - After the code block (
{}) is executed, the process returns to the first step, where theconditionis evaluated. The subsequent steps are repeated.
Usefulness of the while Statement
The while statement is very useful when the number of iterations is not predetermined and a code block ({}) needs to be repeatedly executed based on a Boolean condition.
The following example repeatedly prompts the user to enter a name until a name is entered, and then displays the entered name on the screen.
It clearly demonstrates how useful the while statement is when repeatedly executing a code block based on a Boolean condition without a predetermined number of iterations.
<button id="btn" type="button">Enter Your Name</button>
<script>
const btn = document.getElementById("btn");
/* Register an event so that the user can click the button
and enter a name in the prompt */
btn.addEventListener("click", function() {
let userName = null;
while (!userName) { // Continue executing prompt while userName is a falsy value
/*
* Returns the entered string when text is entered in the prompt and
the OK button is clicked
* Returns an empty string ("") when the OK button is clicked without
entering anything
* Returns null when the Cancel button is clicked
*/
// Assign the return value of prompt to userName
userName = prompt("Please enter your name.");
/*
* If prompt returns the entered string
(when text is entered and the OK button is clicked),
* change the button text
* and disable the button using the disabled attribute
*/
if (userName) {
this.textContent = "Nice to meet you, " + userName + "!";
this.setAttribute("disabled", "");
}
}
});
</script>
Code Explanation
The textContent property gets or sets the text content of an HTML element.
Code Explanation
The setAttribute() function sets an attribute value on a DOM element
If you do not enter a name in the prompt, the prompt continues to appear.
Be Careful!
The example code above is a simple example for understanding how the while statement works.
It is intended to demonstrate how useful the while statement is when repeatedly executing a code block based on a Boolean condition without a predetermined number of iterations. If the user clicks Cancel or does not enter anything, the code can result in an infinite loop. Do not use this example in production!
On the other hand, the for statement is very useful when repeatedly executing code a specific number of times or over a specified range.
Things to Keep in Mind
When using the while statement, be careful not to create an infinite loop.
To control the number of iterations, consider changing the condition expression or using an increment/decrement expression as appropriate.
let i = 1;
while (i <= 5) {
console.log(i);
/* Without the increment/decrement expression in this code, an infinite loop occurs. */
// i++;
}
Specifications
| Specification | |
|---|---|
while
|
ECMAScript® 2027 Language Specification #sec-while-statement |
Compatibility
| Statement |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
Node.js
|
|---|---|---|---|---|---|
while
|
1 | 12 | 1 | 1 | 0.10 |