Ready. Start with one instruction.
Loading the learning workspace…
FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION
Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.
Predict → Experiment → Build → Test → Explain
Ready. Start with one instruction.
Loading the learning workspace…
while checks a condition before running its body. do...while runs the body once before checking. That difference is useful when a first attempt must happen, but it can surprise you at a zero boundary. Both forms need an update that eventually makes the condition false. This countdown uses while so a starting zero produces no output at all.
let remaining = 3;
while (remaining > 0) {
console.log(remaining);
remaining--;
}3 2 1
Decrementing moves remaining toward zero and allows the loop to terminate.
let remaining = 3;Declare remaining with let. The binding may be reassigned later. The expression after = supplies its initial value; = is assignment, not an equality comparison.
while (remaining > 0) {Test the condition before the next iteration. The body must eventually make this false or execution must be stopped.
console.log(remaining);Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
remaining--;Execute this statement at this point in the program. The update changes the current binding or object; compare state before and after.
}Close the current instruction block.
let remaining = 1;
while (remaining > 0) {
console.log(remaining);
remaining--;
}1
Decrementing moves remaining toward zero and allows the loop to terminate.
let remaining = 1;This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
while (remaining > 0) {Test the condition before the next iteration. The body must eventually make this false or execution must be stopped.
console.log(remaining);Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
remaining--;Execute this statement at this point in the program. The update changes the current binding or object; compare state before and after.
}Close the current instruction block.
let remaining = 0;
do {
console.log(remaining);
remaining--;
} while (remaining > 0);0
do...while runs its body once before testing the condition. With the same starting zero, a while loop would run zero times. The final semicolon belongs to the do...while statement.
let remaining = 0;This statement belongs to the operation described above. Compare its names, arguments and current state before advancing.
do {Read this exact statement together with the concept explanation. Track the binding or property it reads or updates before moving to the next line.
console.log(remaining);Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
remaining--;Execute this statement at this point in the program. The update changes the current binding or object; compare state before and after.
} while (remaining > 0);Read this exact statement together with the concept explanation. Track the binding or property it reads or updates before moving to the next line.
Repair the intentionally changed operation so the example produces the exact expected result. Then try the extension without copying the answer.
let remaining = 3;
while (remaining > 0) {
console.log(remaining);
remaining++;
}let remaining = 3;
while (remaining > 0) {
console.log(remaining);
remaining--;
}An update in the wrong direction can create an infinite loop; Stop Execution safely terminates this worker.
Which operation changed the result, why did it work, and what would you test next?
Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.
Related: Previous lesson Next lesson
100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.