← JavaScript

While Loops

Repeats a block of code as long as its condition inside parentheses stays true.

Understanding While Loops in JavaScript

What it does: This line moves count closer to making the condition false. This happens when the loop variable is never updated properly. The loop keeps running until the condition becomes false. JavaScript is case-sensitive, so 'while' must be lowercase. The condition always goes inside round brackets. The block of code to repeat is wrapped in { }. = assigns a value - use a comparison operator like < instead. A while loop can decrease a variable each pass, like n = n - 1.

Common mistakes: 'while' is misspelled as 'Wile'. The condition needs parentheses: while (n > 0).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 11–25) and word search (Modules 1–29).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Does a while loop repeat as long as its condition is true?
YesNo
Matches
while n > 0 { n = n - 1; }
↓
Wrong - the condition needs parentheses
Applying
Meanings
Term
while
Meaning
Repeats a block of code as long as its condition inside parentheses stays true.
Practice
let count = 1;
Testing
Gaps
(count <= 3) {
Check Answer
Spots
1let count = 1;
2Wile (count <= 3) {

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Snow Leopard' badge — Silent Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Does a while loop repeat as long as its condition is true?
A. Yes ✓
B. No
The loop keeps running until the condition becomes false.
2. Must 'while' be written in lowercase?
A. No
B. Yes ✓
JavaScript is case-sensitive, so 'while' must be lowercase.
3. Does a while loop's condition need to be in parentheses?
A. Yes ✓
B. No
The condition always goes inside round brackets.
4. Can forgetting to update a variable cause an infinite loop?
A. Yes ✓
B. No
If the condition never becomes false, the loop runs forever.
5. Does a while loop's code block use curly braces?
A. No
B. Yes ✓
The block of code to repeat is wrapped in { }.

Match the Pairs

while (count <= 3) { ... } → Repeats the block while count is 3 or less
while (isRunning) { ... } → Repeats the block while isRunning stays true
Updates a variable so the loop eventually stops → count = count + 1;
Runs forever because the condition never becomes false → Infinite loop
while n > 0 { n = n - 1; } → Wrong - the condition needs parentheses
while (z = 3) { z = z + 1; } → Wrong - use a comparison operator, not = for assignment

Key Term

while
Repeats a block of code as long as its condition inside parentheses stays true.

Fill in the Blanks

1. while (count <= 3) {
Hint: Type the keyword that starts a while loop.
2. while (count <= 3) {
Hint: Type the operator that checks less than or equal to.
3. while (n > 0) { n = n - 1; }
Hint: Type the operator that subtracts one from n.
4. while (isRunning) {
Hint: Type the keyword that starts a while loop.
5. while (i < 5) { i = i + 1; }
Hint: Type the operator that adds one to i.

Spot the Error

Question 1
A let count = 1;
B Wile (count <= 3) {
C count = count + 1;
Line B — 'while' is misspelled as 'Wile'.
Question 2
A while n > 0 {
B let n = 5;
C n = n - 1;
Line A — The condition needs parentheses: while (n > 0).
Question 3
A let i = 0;
B while (i < 5) {
C console.log(i)
Line C — This line is missing a semicolon, but that's not required in JS.
Question 4
A let x = 10;
B while (x > 0) [
C x = x - 1;
Line B — Code blocks use curly braces {}, not square brackets [].
Question 5
A let y = 1;
B while (y <= 5) {
C y = y + 1;
D ]
Line D — The block must close with }, not ].

Student with a class code? Join your class →