Loops are control-flow structures that repeatedly execute a block of code as long as a specified condition is satisfied โ€” essential for repeating operations a fixed number of times, iterating through arrays and objects, and generating sequences (like Fibonacci numbers).

while do...while for nested arrays for...of objects break / continue
๐Ÿ”ต Concept 2

while Loop

A while loop executes its body as long as its condition evaluates to true. The condition is checked before each iteration โ€” if it's initially false, the body is skipped entirely.

Simple counting example

let i = 0;
while (i < 10) {
  console.log(i);
  i = i + 1;
}
โ€บ0 1 2 3 4 5 6 7 8 9

Once i reaches 10, i < 10 is false and the loop stops โ€” 10 never prints.

Searching in an array with while

let someArray = ["Mike", "Antal", "Marc", "Emir", "Louiza", "Jacky"];
let notFound = true;
while (notFound && someArray.length > 0) {
  if (someArray[0] === "Louiza") {
    console.log("Found her!");
    notFound = false;
  } else {
    someArray.shift();
  }
}
console.log(notFound);
โ€บFound her! false

Each pass removes the first element with shift() until "Louiza" reaches index 0. The length check prevents an infinite loop if the name were never found.

Generating the Fibonacci sequence

let nr1 = 0;
let nr2 = 1;
let temp;
let fibonacciArray = [];
while (fibonacciArray.length < 25) {
  fibonacciArray.push(nr1);
  temp = nr1 + nr2;
  nr1 = nr2;
  nr2 = temp;
}
console.log(fibonacciArray);
โ€บ[ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368 ]
๐ŸŸฃ Concept 3

do...while Loop

A do...while loop guarantees its body runs at least once, because the condition is checked after the body runs โ€” useful for input prompts or connection attempts.

do {
  // code to execute at least once
} while (condition);

Validating user input between 0 and 100

let number;
do {
  number = prompt("Please enter a number between 0 and 100");
} while (!number || number < 0 || number > 100);
console.log("You entered:", number);
๐Ÿ’ฌ This depends on live user input via prompt(), so there's no fixed output โ€” !number catches an empty/cancelled prompt; the other two conditions catch out-of-range values.

Control flow

Start
  |
  v
Execute body
  |
  v
Evaluate condition
  |
  +-- true  --> repeat
  +-- false --> exit
๐ŸŸข Concept 4

for Loop

A for loop packs initialization, condition, and update into one header. Execution order: initialize once โ†’ check condition โ†’ run body โ†’ run update โ†’ repeat from the condition check.

for (let i = 0; i < 10; i = i + 1) {
  console.log(i);
}
โ€บ0 1 2 3 4 5 6 7 8 9

Array of integers 0 to 99

let arr = [];
for (let i = 0; i < 100; i = i + 1) {
  arr.push(i);
}
console.log(arr);
โ€บ[ 0, 1, 2, ... , 98, 99 ] (100 items total)

Array of even numbers from 0 to 98

let arr = [];
for (let i = 0; i < 100; i = i + 2) {
  arr.push(i);
}
console.log(arr);
โ€บ[ 0, 2, 4, ... , 96, 98 ] (50 items total)
๐ŸŸ  Concept 5

Nested Loops

A nested loop is a loop inside another loop โ€” commonly used for two-dimensional data such as tables, matrices, or arrays of arrays.

Building a 3ร—7 array of arrays

let arrOfArrays = [];
for (let i = 0; i < 3; i = i + 1) {
  arrOfArrays.push([]);
  for (let j = 0; j < 7; j = j + 1) {
    arrOfArrays[i].push(j);
  }
}
console.log(arrOfArrays);
โ€บ[ [ 0, 1, 2, 3, 4, 5, 6 ], [ 0, 1, 2, 3, 4, 5, 6 ], [ 0, 1, 2, 3, 4, 5, 6 ] ]

Outer loop runs 3 times (rows); inner loop runs 7 times per row (columns 0โ€“6). console.table(arrOfArrays) displays this neatly in supporting consoles.

๐Ÿฉท Concept 6

Loops and Arrays

let arr = /* some array */;
for (let i = 0; i < arr.length; i = i + 1) {
  // use arr[i]
}

Logging each element

let names = ["Chantal", "John", "Maxime", "Bobbi", "Jair"];
for (let i = 0; i < names.length; i = i + 1) {
  console.log(names[i]);
}
โ€บChantal John Maxime Bobbi Jair

Modifying every element

let names = ["Chantal", "John", "Maxime", "Bobbi", "Jair"];
for (let i = 0; i < names.length; i = i + 1) {
  names[i] = "hello " + names[i];
}
console.log(names);
โ€บ[ 'hello Chantal', 'hello John', 'hello Maxime', 'hello Bobbi', 'hello Jair' ]

Filtering with delete + continue

let names = ["Chantal", "John", "Maxime", "Bobbi", "Jair"];
for (let i = 0; i < names.length; i = i + 1) {
  if (names[i].startsWith("M")) {
    delete names[i];
    continue;
  }
  names[i] = "hello " + names[i];
}
console.log(names);
โ€บ[ 'hello Chantal', 'hello John', <1 empty item>, 'hello Bobbi', 'hello Jair' ]
๐Ÿ’ก delete leaves an empty slot rather than reindexing โ€” different from splice(), which shifts later elements down.

Infinite loop hazard

let names = ["Chantal", "John", "Maxime", "Bobbi", "Jair"];
for (let i = 0; i < names.length; i = i + 1) {
  names.push("...");   // โŒ grows names.length every iteration
}
๐Ÿšซ push() keeps growing names.length, so i < names.length is never false โ€” this never terminates.
๐Ÿ”ท Concept 7

for...of Loop

Iterates directly over the values of an iterable (like an array), with no index variable needed.

let names = ["Chantal", "John", "Maxime", "Bobbi", "Jair"];
for (let name of names) {
  console.log(name);
}
โ€บChantal John Maxime Bobbi Jair

Ideal for reading/processing values โ€” but doesn't give you an index for in-place modification the way a classic for loop does.

โšช Concept 8

Loops and Objects (conceptual)

For arrays, prefer index-based for loops or for...of when you just need values. For objects, use for...in to iterate over enumerable keys, or convert to an array first with Object.keys(), Object.values(), or Object.entries() and then use an array loop.

๐Ÿ”ด Concept 9

break & continue

  • break โ€” immediately terminates the nearest enclosing loop; useful once you've found what you were searching for.
  • continue โ€” skips the rest of the current iteration and moves to the next iteration's condition check; useful for filtering out certain elements.