Back to JavaScript Notes
Topic #291

The while Loop

You will be able to use a JavaScript while loop to repeat a block of code until a condition becomes false, while updating the condition safely to avoid infinite loops.

What it is

A while loop is a control-flow statement that checks a condition before each repetition. If the condition is true, JavaScript runs the loop body. If it is false, the loop stops and execution continues after the loop. The mental model is: “keep doing this as long as this remains true.” Related terms include loop body, condition, counter, sentinel value, and infinite loop.

Unlike a for loop, a while loop does not require the initialization, condition, and update to be written in one line. This makes it useful when the number of repetitions is unknown at the start.

Why it matters

  • It lets you process data until a stopping point, such as reading items from an array or list.
  • It supports waiting for a state change, such as polling until a value is found.
  • It helps implement game loops, retry logic, and input validation.
  • It makes code shorter than copying the same statements many times.
  • It teaches careful condition design, which is important for avoiding infinite loops.

Syntax or steps

The smallest useful pattern has three parts: create a variable, test it, and change it inside the loop.

let counter = 0;

while (counter < limit) {
  // loop body
  counter++;
}

Steps: initialize counter before the loop; write a condition that can become false; run the body; update the variable that affects the condition. If the condition never changes, the loop may never end.

Example

This example prints numbers from 0 to 2 and then stops.

let i = 0;

while (i < 3) {
  console.log(i);
  i++;
}

Part by part:

  • let i = 0; creates a counter starting at zero.
  • while (i < 3) checks whether i is still less than three.
  • console.log(i); prints the current value.
  • i++; increases i by one, moving the loop toward its stopping condition.

The loop runs three times: when i is 0, 1, and 2. When i becomes 3, the condition is false, so the loop exits.

Common mistakes

  • Forgetting to update the condition variable. If i never changes, i < 3 stays true. Fix: include an update such as i++ or change the value being tested.
  • Starting with a condition that is already false. The loop body may never run. Fix: check the initial value and decide whether that behavior is intended.
  • Using the wrong comparison. i <= 3 runs one extra time compared with i < 3. Fix: count the expected number of repetitions.
  • Putting the update outside the loop. If the update is after the closing brace, it runs only once. Fix: place the update inside the loop body.

When to use it

Use a while loop when you do not know how many repetitions are needed, or when the stopping condition depends on changing data. Use a for loop when the number of repetitions is known and you want initialization, condition, and update grouped together.

LoopBest whenExample condition
whileRepeating until a state changeswhile (found === false)
forRepeating a known number of timesfor (let i = 0; i < 3; i++)

Practice

Guided exercise: write a while loop that prints the numbers 1 through 5.

let n = 1;

while (n <= 5) {
  console.log(n);
  n++;
}

Expected output: 1 2 3 4 5, each on its own line.

Challenge: use a while loop to count down from 3 to 1. Hint: start at 3, test n > 0, and use n--.

Quick check

Question: What must be true for a while loop to stop?

Answer: Its condition must eventually become false, usually because the loop body changes a variable used in the condition.

Summary

A while loop repeats code as long as a condition is true. The key transferable idea is to design a clear starting state, a stopping condition, and an update that moves the loop toward that condition.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

The while Loop – FAQs

Quick answers about learning The while Loop in JavaScript.

This free note from Coding Now Tech Institute explains The while Loop in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including The while Loop, is 100% free with no signup required.
With focused practice, most students grasp The while Loop in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now