π INDEX
LOADING ENGINE...
JS while Loop
Learn how to repeat actions until a condition is met. Master the art of iteration without crashing your computer!
loop.js
1 / 3
π
Looping Logic Simulation...
Tutor: A 'while' loop repeats a block of code as long as a specified condition is true. It starts with the 'while' keyword followed by a condition in parentheses.
How it works
1. The Condition
Before every "lap", JavaScript checks if the condition is true. If it is, the loop runs again.
2. The Update
You must update a variable inside the loop (like i++) so the condition eventually becomes false.
// Example
let fuel = 3;
while (fuel > 0) {
console.log("Flying...");
fuel--;
}
// Output: Flying... (3 times)
Mission Briefing
π
While Master
Successfully created a controlled while loop.
π§
Logic Pro
Understand loop conditions and increments.
π‘οΈ
Infinite Survivor
Avoided the dreaded infinite loop.