While Loop Countdown
Predict Output Medium
Question
What does this code print?
Code
let n = 3;
while (n > 0) {
console.log(n);
n--;
}
Hint
The loop runs while n > 0, printing n then decrementing.
Explanation
n starts at 3. First iteration: prints 3, n becomes 2. Second: prints 2, n becomes 1. Third: prints 1, n becomes 0. Now n > 0 is false, loop ends. The output is: 3, then 2, then 1 (each on its own line).
Related Resources
Related Lessons
- Loops (for & while)
Learn how for and while loops work.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.