JavaScript Tutorial

JavaScript While

while repeats while a test is true. do...while runs the body once before the first test.

Repeat while a test is true

A while loop checks a test, runs a block, then checks again. As long as the test is true, the block repeats. When the test is false, JavaScript leaves the loop and continues with the next statement.

Something inside the loop must change the values the test looks at. If nothing changes, the test never becomes false and the script does not stop.

A complete while loop

This loop prints 1 through 5. n starts at 1. Each pass records the current value, then adds 1. When n becomes 6, the test is false and the loop ends.

Example

let n = 1;
const lines = [];

while (n <= 5) {
  console.log(n);
  lines.push(String(n));
  n = n + 1;
}

document.body.textContent = lines.join(", ");

Run this in /javascript/try. Then change 5 to another small number. Keep the increment. A missing n = n + 1 is how beginners freeze the tab.

The three parts you manage

  1. Set a starting value before the loop.
  2. Write a test that can become false.
  3. Update that value inside the loop.

Forget step 3 and the test stays true forever. Forget step 1 and the test uses undefined. Thefor loop packs these three into one line. while leaves them visible, which is useful when the update is not a simple plus one — waiting for a flag, shrinking a number, or draining a queue.

Drain a list

while (queue.length > 0) is a natural shape: the test is “is there still work?”shift removes the front item. Length falls. Eventually the test fails.

Example

const queue = ["draft", "review", "ship"];
const done = [];

while (queue.length > 0) {
  const item = queue.shift();
  console.log(item);
  done.push(item);
}

document.body.textContent = "done: " + done.join(", ");

do...while runs the body first

A do...while loop runs the block once, then checks the test. If the test is true, it runs again. The body always executes at least once, even when the test starts false.

Example

let n = 8;
const lines = [];

do {
  console.log(n);
  lines.push(String(n));
  n = n + 1;
} while (n <= 5);

document.body.textContent = "ran once: " + lines.join(", ");

n starts at 8, which already fails n <= 5. A plain while would skip the body. do...while still records 8, then checks and stops.

while versus for

ShapeWhen to write it
forYou know the count, or you have an index.
whileYou repeat until a condition changes.
do...whileYou must run the body once before testing.

Infinite loops lock the preview. If the page stops responding, check that the variable in the test actually changes, and that the test can fail. Reload the editor and try a smaller bound.

What to remember

  • Initialize before the loop. Update inside it. The test must be able to fail.
  • while checks first. do...while runs once, then checks.
  • A shrinking list or a rising counter are the usual ways to stop.
  • Next chapter: break leaves now; continue skips to the next pass.

Next: break and continue — leave a loop early, or skip the rest of this round.

FAQ: JavaScript While

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript while loop examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript while loop in this JavaScript JavaScript lesson (JavaScript While).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.