Skip to main content
Module 5 · Lesson 1 of 417/32 in the course~16 min
Module lessons (1/4)

Loops: for and while

Lesson preview5 hands-on exercises readyJavaScript Course · Module 5 — Control flow

classic for, while, do…while: repeat until a condition.

What you'll learn in this lesson

  • Choose with confidence between classical for (counting with index), while (condition unknown a priori) and do...while (at least one guaranteed iteration).
  • Avoid off-by-one conditions (< length vs <= n) and infinite loops by always updating the exit state.
  • Understanding the scope per iteration of let in loops versus the historical bug of var.
  • Decide when to use an imperative loop instead of an array method like .map or .filter.

Overview

Loops repeat a block of code as long as a condition remains true. Knowing how to read the counter, step and exit condition avoids classic errors such as skipping the last element (off-by-one) or creating infinite loops.

Sections covered in this lesson

  1. 1Mental model: choosing the right cycle
  2. 2Progressive examples
  3. 3Guided laboratory
  4. 4Check for understanding
Pro Lesson · Module 5 — Control flow

Unlock "Loops: for and while" and the full JavaScript Course curriculum

classic for, while, do…while: repeat until a condition.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in JavaScript Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.