JS: for while do Loop

By Xah Lee. Date: . Last updated: .

“for” Loop

for (let i=0; i < 9; i++) { console.log(i); }

“while” Loop

let x = 0;
while (x !== 5) { console.log(x); x++;} // prints 0 to 4

“do…while” Loop

let x = 0;
do { console.log(x); x++} while (x !== 5); // prints 0 to 4

“continue”, “break”

for (let i=0; i <= 5; i++) {
    if (i===3) {continue;}       // skip 3
    console.log(i);
}                               // prints 0 to 5, but not 3
for (let i=0; i < 5; i++) {
    console.log(i);
    if (i===3) {break;}
}                               // prints 0 to 3

for-in Loop and for-of Loop

for-in Loop

This is for going thru a object's properties, including parent object's properties.

You need to understand properties and inheritance first.

JS: for-in Loop

for-of Loop

This is for going thru iterable objects.

You need to understand iterable object first.

JS: for-of Loop

JavaScript Basics

  1. JavaScript Basics
  2. true, false
  3. Operators
  4. Branch Control
  5. Loop
  6. Array Basics
  7. Object Basics
  8. RegExp Basics
Like it? Help me by telling your friends. Or, Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. CSS
  3. JavaScript
  4. JS Obj Ref
  5. DOM
  6. SVG
  7. Blog