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 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;
}
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);
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);
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);
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
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);
}
Array of integers 0 to 99
let arr = [];
for (let i = 0; i < 100; i = i + 1) {
arr.push(i);
}
console.log(arr);
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);
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);
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.
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]);
}
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);
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);
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.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);
}
Ideal for reading/processing values โ but doesn't give you an index for in-place modification the way a classic for loop does.
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.
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.