Loops are used to iterate a piece of code by repeatedly running a block until a condition is met.
Loop that runs a set number of times with initialization, condition, and update steps.
for (initialisation; condition; updation){
// code
}Loop inside another loop. Used for 2D arrays or patterns.
for (let i = 1; i <= 3; i++) {
for (let j = 1; j <= 3; j++) {
// code
}
}An infinite loop is a loop that never ends because its exit condition is never met or it lacks an exit condition altogether. This causes the loop to run indefinitely, which can freeze or crash programs if not controlled.
// Example
for (let i = 1; i >= 0; i++) {
}
for (let i = 1; i <= 5; i--) {
}
for (let i = 1; ; i++) {
}Runs while condition is true. Use when iterations are unknown.
while (condition) {
// code
}Stops the loop immediately and exits it.
break;Iterate through array elements using index.
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}Loop through 2D arrays (arrays inside arrays).
for (let i = 0; i < outerArray.length; i++) {
for (let j = 0; j < outerArray[i].length; j++) {
console.log(outerArray[i][j]);
}
}A for...of loop operates on the values sourced from an iterable (like arrays, strings, maps, sets, etc.) one by one in sequential order.
for (let item of array) {
// code
}For 2D arrays (arrays inside arrays).
for (let outer of twoDArray) {
for (let inner of outer) {
// code
}
}