JavaScript / Loops

The while loop

Choose between let and const, use !=, and repeat a task as long as a condition is true with a while loop.

let or const?

Some values change while a program runs, like how much money you have. Others never change, like the number of days in a week. Declare a value that will change with let, and a fixed value with const (a constant).

JavaScript
const daysInWeek = 7;
let day = 30;
console.log("A week has " + daysInWeek + " days");
console.log("September has " + day + " days");
day = 31;
console.log("October has " + day + " days");
Console
A week has 7 days
September has 30 days
October has 31 days
A constant cannot be given a new value after it is declared, which protects it from being changed by mistake. It must also get its value on the same line, so const num; is an error too.
JavaScript
const num = 3;
num = 5;
console.log(num);
Console
TypeError: Assignment to constant variable.

The not-equal operator !=

A != B means "A is not equal to B". The condition is true when the two values are different:

JavaScript
const weather = "Cloudy";
if (weather != "Rainy") {
  console.log("Play soccer");
}
Console
Play soccer

The while loop

A for loop suits a known number of repeats. Sometimes we do not know it: "keep buying comics as long as you have enough money". Then we use while (condition) { ... }: it checks the condition, runs the code in the braces if it is true, then checks again.

JavaScript
let money = 2000;
const comics = 425;
while (money > comics) {
  money = money - comics;
  console.log(money);
}
Console
1575
1150
725
300

We can count the repeats with a variable that starts at 0. Here we save 15000 each month until we reach 85000. Note that money += 15000 is short for money = money + 15000:

JavaScript
let money = 30000;
let month = 0;
while (money < 85000) {
  month++;
  money += 15000;
}
console.log("Save for " + month + " months");
Console
Save for 4 months
LoopUse it when
forwe know how many times to repeat
whilewe repeat until a condition changes, without knowing the count
The code inside the loop must change something in the condition (like money). If you forget, the condition stays true forever and the loop never ends.

Practice

1Fill in the blankCurriculum practice

Write let or const in the blank. Careful: day will change to 31.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
___ day = 30;
console.log("September has " + day + " days");
day = 31;
console.log("October has " + day + " days");
Console
Press Run to see the result.
2Edit the programCurriculum practice

Run the program and you will see an error. Fix the declaration on the first line so it prints 5.

Hintnum changes, so it is not a constant.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const num;
num = 3;
num = 5;
console.log(num);
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Write the condition: if myScore is not equal to 100, print Aim for a perfect score!

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const myScore = 90;
if (___) {
  console.log("Aim for a perfect score!");
}
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Complete the loop: buy a snack and print the money left as long as money is greater than snacks.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let money = 1000;
const snacks = 230;
___ (___) {
  money = money - snacks;
  console.log(money);
}
Console
Press Run to see the result.
5Fill in the blankCurriculum practice

You save 800 a month. Complete the loop so it keeps saving as long as money is less than 4000; it prints Save for 5 months.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let money = 600;
let month = 0;
___ (___) {
  month++;
  money += 800;
}
console.log("Save for " + month + " months");
Console
Press Run to see the result.
6Write the programExtra practice

A novel has 230 pages and you read 40 pages a day. Use while to find the number of days, then print Finished in 6 days (with the number worked out by the loop).

HintRepeat while pages > 0: subtract 40 and add 1 to days.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let pages = 230;
let days = 0;
Console
Press Run to see the result.
7Write the programExtra practice

One student knows a piece of school news, and the number who know it doubles every hour. Start n at 1; print n, then double it, as long as n is less than 100. (It prints 1, 2, 4 up to 64, one per line.)

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • let for a value that will change, const for a fixed one; giving a constant a new value is an error.
  • A != B is true when A is not equal to B.
  • while (condition) { ... } repeats as long as the condition is true; use it when the count is unknown.
  • Change something in the condition inside the loop, or it never stops.