Ch 7 · Loops & Iterations · JavaScript

Topic 7 of 13 in JavaScript — Complete Tutorial (Beginner) — 5 lessons.

Why We Need Loops

Computers are exceptionally good at one thing that humans find tedious: repetition. A computer can perform the exact same operation a million times in a fraction of a second, without getting tired, without making mistakes, and without complaining. A loop is a programming construct that makes a block of code execute repeatedly, automatically, without you having to write it out multiple times. Without loops, if you wanted to print the numbers from 1 to 100, you would need to write 100 separate lines of code. With a loop, you write the instruction once and tell JavaScript how many times to repeat it — that is three lines of code no matter how many repetitions you need.

The for Loop

The for loop is the most commonly used loop in JavaScript. Its defining feature is that all three pieces of the loop machinery — where to start, when to stop, and how to step — are written together on one line, making it very easy to read and understand at a glance. The initialiser creates and sets the counter variable, the condition specifies how long to keep looping (the loop runs while this is true), and the update describes what to do to the counter after each repetition. JavaScript checks the condition before every single repetition — the moment it becomes false, the loop ends and execution continues with the code below.

The while Loop

The while loop continues executing its body as long as a specified condition remains true. Unlike the for loop, the while loop does not have a built-in counter — you manage the update yourself inside the loop body. This makes it more flexible: the stopping condition does not have to be based on counting at all. It could be based on receiving a particular value, reaching a certain total, or any other dynamic condition. JavaScript checks the condition before every single repetition — if the condition is false from the very start, the loop body never runs even once.

The do...while Loop

The do-while loop is similar to the while loop, but with one key difference: the body of the loop runs first, and the condition is checked afterwards. This guarantees the loop body executes at least once, regardless of whether the condition is true or false at the start. This makes it ideal for situations where you always need to perform an action at least one time before deciding whether to repeat it — for example, always showing a menu to the user and then asking if they want to continue, or always making one attempt at a task before checking if it succeeded.

Loop Control: break and continue

Sometimes you need to exit a loop before it naturally finishes, or skip a particular repetition without stopping the whole loop. The break keyword immediately terminates the loop and moves execution to the code that comes after the loop. It is most useful when you are searching through data and have found what you were looking for — there is no point checking the remaining items once the answer is found. The continue keyword skips the rest of the current repetition and jumps directly to the next one — it is useful for ignoring certain values while still processing all others.

All topics in JavaScript Beginner

  1. Introduction to Programming & JavaScript
  2. Your First JavaScript Program
  3. Variables — Your Data Containers
  4. Data Types — Kinds of Values
  5. Operators & Expressions
  6. Conditional Statements
  7. Loops & Iterations
  8. Functions
  9. Arrays — Lists of Data
  10. Objects & Classes
  11. Strings — Working with Text
  12. Error Handling
  13. Mini-Projects & Practice