while Loop

let i = 1;

while(i <= 5){
   console.log(i);
   i++;
}

Structure

while(condition){
   // code
}

The condition is checked before executing the loop body.

The for loop simply puts initialization, condition, and increment in one line.


for → Use when you know how many times to loop.

while → Use when looping depends on a condition.