> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript While Loop

The `while` statement creates a control flow loop that executes a specified block of code repeatedly as long as a given test condition evaluates to `true`. Because the condition is evaluated before the execution of the loop body, it is classified as a pre-test loop.

```javascript theme={"dark"}
while (condition) {
  // statement(s)
}
```

## Execution Mechanics

1. **Condition Evaluation:** The `condition` expression is evaluated before each iteration. JavaScript applies implicit boolean type coercion to the result.
2. **Truthy Path:** If the condition evaluates to a truthy value, the `statement` block executes.
3. **Falsy Path:** If the condition evaluates to a falsy value (e.g., `false`, `0`, `""`, `null`, `undefined`, `NaN`), the loop terminates immediately. Control flow passes to the first statement following the `while` block.
4. **Iteration:** After the `statement` block completes, control jumps back to Step 1.

Because it is a pre-test loop, if the condition evaluates to a falsy value on the very first check, the loop body will execute exactly zero times.

## Basic Implementation

```javascript theme={"dark"}
let iteration = 0;

while (iteration < 3) {
  console.log(iteration);
  iteration++; 
}
// Output: 0, 1, 2
```

In this structure, the variable evaluated in the condition (`iteration`) must be mutated within the loop body. Failing to alter the state of the condition variable results in an **infinite loop**, blocking the main thread.

## Loop Control Statements

You can alter the standard execution flow of a `while` loop using specific control statements:

* **`break`**: Immediately terminates the loop entirely, bypassing the condition check, and transfers control to the next statement outside the loop.
* **`continue`**: Immediately halts the current iteration. Control jumps directly back to the condition evaluation to determine if the next iteration should begin.

```javascript theme={"dark"}
let count = 0;

while (count < 5) {
  count++;
  
  if (count === 2) {
    continue; // Skips the rest of the block, jumps to condition
  }
  
  if (count === 4) {
    break; // Terminates the loop entirely
  }
  
  console.log(count);
}
// Output: 1, 3
```

## Type Coercion in Conditions

The `while` loop does not require a strict boolean primitive. It relies on JavaScript's internal truthiness evaluation.

```javascript theme={"dark"}
let val = 3;

// The integer 3 is truthy. 
// The loop runs until val is decremented to 0 (which is falsy).
while (val) {
  val--;
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor JavaScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
