4.4 While Loops
In the previous lesson we used a for loop to print every odd number between 0 and 5. The while loop can produce the exact same result with a slightly different shape. The key difference is that a for loop declares its loop variable inline, whereas a while loop expects the variable to be declared outside the loop.
Translating the for loop
let i = 0;
while (i <= 5) {
if (i % 2 !== 0) console.log(i);
i++;
}
We start by declaring i and initializing it to 0. The while statement takes a condition between parentheses; the body runs as long as the condition stays true. Inside the body, the if only prints the value when i is odd, and the final i++ moves us to the next iteration.
- Initialization is done externally, before the loop.
- Condition is checked first; if it's already false, the body never runs.
- Increment is your responsibility inside the body — forgetting it creates an infinite loop.
Note that the i declared outside the while is distinct from the i declared inside a for loop — the latter is scoped to the loop body. We'll cover scope in detail later. In the next video, we'll look at another type of loop in JavaScript.
Summary
This lesson explains while loops in JavaScript and their key differences from for loops. While loops require you to declare the loop variable externally and manage the increment explicitly inside the loop body. The lesson demonstrates the syntax and step-by-step execution of a while loop that iterates through numbers, showing when the condition is evaluated and when the loop body executes or terminates.
Key points
- While loops require external variable declaration (unlike for loops where the variable is part of the loop syntax)
- The loop condition is evaluated at the start of each iteration; if true, the body executes; if false, the loop terminates
- You must manually increment the loop variable inside the while block to avoid infinite loops
- While loops have different variable scope than for loops—variables declared outside the loop are accessible throughout the program
- The structure consists of: variable initialization, while keyword with condition, loop body with statements, and explicit increment
- While loops are useful when you don't know in advance how many iterations are needed, making them more flexible than traditional for loops
FAQ
What is the main difference between a while loop and a for loop in JavaScript?
In a for loop, the loop variable, condition, and increment are all declared in one line as part of the loop syntax. In a while loop, you must declare the variable externally, specify only the condition in parentheses, and manually include the increment statement inside the loop body.
Why do I need to increment the loop variable inside a while loop?
You must increment the loop variable inside the while block to ensure the condition eventually becomes false. Without incrementing, the condition would remain true indefinitely, creating an infinite loop that would never terminate.
When should I use a while loop instead of a for loop?
Use a while loop when you don't know the exact number of iterations in advance or when the loop should continue based on a complex condition rather than a simple counter. While loops are more flexible for conditional logic that determines when to exit the loop.