JavaScript lesson 12: while loops
US version. US grades and US spelling. · UK version of this lesson
while (condition) { … } repeats the block as long as the condition is true. It checks BEFORE every pass, so the block might not run at all. A complete 40-minute JavaScript lesson for beginners (Grades 7–8), from Unit 5: Loops. It runs right here in your browser – nothing to install.
- Time
- 40 minutes + 20 min homework
- Grade
- Grades 7–8 (ages 12–13)
- You need
- A computer or tablet, paper and a pencil
- Graded
- Instantly, with explanations
For parents: how to use this lesson
You don't need to know JavaScript yourself. The lesson explains each step on screen, the way Mr Babbage, our Head of Computing, likes it: a worked example first, then a go for your child. Code questions run your child's program in the browser – graded straight away.
- Read what your child will learn together.
- Press Start the lesson below. Sit with your child for the teaching and the worked example (about 16 minutes): ask them to say what each line does.
- Let them do the practice on their own. When code goes wrong, the error message is a clue – let them read it out and try again before you help.
- Finish with the talk question: Why might a while loop run for ever? How do you stop that happening?
Watch out for
- A while loop always runs at least once. Try a condition that is false at the start.
- The loop stops by itself. Something inside must change the condition.
Miss Lovelace's tip for home: ask your child to explain one line of their program to you in their own words. If they can explain it, they understand it – and every error message is a clue, not a failure.
What your child will learn
Learning goal: repeat code with a while loop.
Success looks like
- I can repeat code with a
whileloop. - I can stop a loop with its condition or
break. - I can spot and fix a loop that never ends.
Key words
while · condition · infinite loop · break · sentinel
What to have ready
- A device with a web browser (the code runs in the browser – nothing to install)
- Paper for tracing
- A keyboard helps for typing code (a tablet or phone works too)
The big ideas (for the adult)
while (condition) { … } repeats the block as long as the condition is true. It checks BEFORE every pass, so the block might not run at all.
Something inside the loop must change, or the loop runs for ever. On Classfern a never-ending loop is stopped for you, with a message.
Use for when you know how many times to repeat; use while for "until something happens", like reading numbers until the user types 0.
Worked example – see it
let x = 5
while (x > 0) {
console.log(x)
x = x - 2
}It prints:
5 3 1
The lesson, step by step
| Part | Time | How it is checked |
|---|---|---|
| Starter | 5 min | Graded by Classfern |
| Teaching input | 8 min | Read, watch and talk |
| See it, then predict | 8 min | Graded by Classfern |
| Put it in order, write it, fix it | 14 min | Graded by Classfern |
| Closure and exit ticket | 5 min | Graded by Classfern + self-check |
The lesson
It runs here on the page. No sign-up and no names – progress is kept only in this browser. Each visit picks a new mix of questions.
Homework
20 minutes: 7 questions – predict what code prints, write or fix a short program, and check the key ideas – then a short reflection. New questions every time, all graded instantly.
Practice tests
Curriculum
CSTA K–12 Computer Science Standards (2017), grades 6–8: 2-AP-12 – Design and iteratively develop programs with control structures incl. nested loops and compound conditionals.
The standard codes are CSTA's; the descriptions are our own words.
Our lessons and questions are our own. They are not written or endorsed by any government department, testing agency or exam board. JavaScript is a trademark or registered trademark of Oracle Corporation in the U.S. and other countries; Classfern is not affiliated with or endorsed by it.