JS MASTER CLASS /// ITERATION /// WHILE AND FOR /// DONT REPEAT YOURSELF /// JS MASTER CLASS /// ITERATION /// WHILE AND FOR /// DONT REPEAT YOURSELF ///

JavaScript Loops

Learn how to execute code repeatedly. Master the `for` and `while` loops, and understand how to avoid infinite crashes.

loops.js
1 / 11
12345
Terminal / Console
...waiting for execution...

Tutor:Programming is all about efficiency. Imagine you need to print 'Hello' 5 times. You could write console.log('Hello') five times... but that violates the DRY principle (Don't Repeat Yourself).


Skill Matrix

UNLOCK NODES BY MASTERING LOOPS.

Concept: Repetition

Loops are handy, if you want to run the same code over and over again, each time with a different value.

System Check

Why do we use loops in JavaScript?


Community Holo-Net

Showcase Your Iterations

ACTIVE

Built an awesome nested loop or algorithm? Share your code snippets in our community.

JavaScript Loops

Author

Pascual Vila

Frontend Instructor // Code Syllabus

Loops offer a quick and easy way to do something repeatedly. Without loops, processing an array of 1,000 items would require 1,000 lines of identical code.

The for Loop

The for loop is the most common and concise way to write a loop when you know exactly how many times you want it to run. It groups the initialization, condition, and increment all on one line.

for (let i = 0; i < 5; i++) {
  console.log("Iteration number " + i);
}

The while Loop

The while loop executes its statements as long as a specified condition evaluates to true. It is ideal for situations where you don't know beforehand how many times the loop needs to run (for example, reading data until a file ends).

let n = 0;
while (n < 3) {
  console.log(n);
  n++; // CRITICAL: Without this, it's an infinite loop!
}

Control Flow: break & continue

Sometimes you need to alter the loop's natural flow:

  • Break: Completely exits the loop, moving code execution to the next line after the loop structure.
  • Continue: Skips the rest of the current iteration and jumps immediately back up to evaluate the condition for the next iteration.
The Infinite Loop Danger+

An infinite loop occurs when a loop's condition always evaluates to true. It will run endlessly, consuming all available CPU and memory resources until the browser tab crashes. Always double-check your increment logic inside while loops!

Loops Glossary

Iteration

A single execution of a set of instructions contained within a loop.

snippet.js

Initialization

The first part of a for loop where the counter variable is declared and set to an initial value.

snippet.js

Condition

The boolean expression evaluated before each loop pass. If true, the loop runs. If false, it terminates.

snippet.js

Increment

The statement executed at the end of every loop iteration, typically used to update the counter.

snippet.js

Infinite Loop

A loop that never terminates because its condition never becomes false. Causes browser crashes.

snippet.js

Break

A keyword used to prematurely exit a loop entirely, regardless of the loop condition.

snippet.js