Predict while Loop Output
Predict Output Easy
Question
What does this code print?
Code
let n = 3;
while (n > 0) {
console.log(n);
n--;
}
Hint
n starts at 3 and decreases by 1 each time.
Explanation
n starts at 3. The loop runs while n > 0: first n=3 (prints 3, n becomes 2), then n=2 (prints 2, n becomes 1), then n=1 (prints 1, n becomes 0). Now n=0 fails the condition, so the loop stops. Output: 3, 2, 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.