Looping Statements are used to execute a block of code repeatedly until a specified condition becomes false.
Loops help developers:
- Avoid writing repetitive code.
- Iterate through arrays and objects.
- Perform repetitive tasks efficiently.
- Reduce code length and improve readability.
JavaScript provides the following looping statements:
- for Loop
- while Loop
- do...while Loop
- for...of Loop
- for...in Loop
The for loop is used when the number of iterations is known in advance.
for(initialization; condition; increment/decrement){
// code to execute
}for(var i = 0; i < 5; i++){
console.log(i);
}0
1
2
3
4
var i = 0Initialization happens once.
i < 5Condition is checked before every iteration.
i++Increments the value by 1 after every iteration.
Initialization
↓
Condition
↓
True → Execute Loop Body
↓
Increment / Decrement
↓
Condition Again
↓
False → Exit Loop
for(var i = 1; i <= 10; i++){
console.log(i);
}1
2
3
4
5
6
7
8
9
10
The while loop executes as long as the condition remains true.
while(condition){
// code
}var i = 0;
while(i < 5){
console.log(i);
i++;
}0
1
2
3
4
The loop continues until:
i < 5becomes false.
var i = 2;
while(i <= 10){
console.log(i);
i += 2;
}2
4
6
8
10
The do...while loop executes the block at least once, even if the condition is false.
do{
// code
}
while(condition);var i = 0;
do{
console.log(i);
i++;
}
while(i < 5);0
1
2
3
4
var i = 10;
do{
console.log(i);
}
while(i < 5);10
Even though:
10 < 5is false,
the loop executes once because the condition is checked after execution.
| while | do...while |
|---|---|
| Condition checked first | Code executes first |
| May execute zero times | Executes at least once |
| Entry Controlled Loop | Exit Controlled Loop |
An Infinite Loop is a loop that never ends because the condition never becomes false.
for(var i = 0; i >= 0; i++){
console.log(i);
}Here:
i >= 0is always true.
Therefore the loop never stops.
var i = 0;
while(i >= 0){
console.log(i);
i++;
}The condition is always true.
var i = 0;
do{
console.log(i);
i++;
}
while(i >= 0);The loop continues forever.
You can stop a loop using:
break;Example:
for(var i=0; i<100; i++){
if(i==5){
break;
}
console.log(i);
}0
1
2
3
4
The for...of loop is used to iterate over:
- Arrays
- Strings
- Iterable Objects
for(variable of iterable){
// code
}var arr = [1,2,3,4,5];
for(var i of arr){
console.log(i);
}1
2
3
4
5
var name = "Basha";
for(var ch of name){
console.log(ch);
}B
a
s
h
a
The for...in loop is used to iterate over:
- Object Properties
- Non-Iterable Objects
for(variable in object){
// code
}var obj = {
name : "John",
age : 30,
city : "New York"
};
for(var i in obj){
console.log(i + " : " + obj[i]);
}name : John
age : 30
city : New York
| for...of | for...in |
|---|---|
| Iterates values | Iterates keys |
| Used with Arrays | Used with Objects |
| Works with iterable objects | Works with object properties |
| Returns values | Returns property names |
var arr = [10,20,30];
for(var i in arr){
console.log(i);
}Output:
0
1
2
Because it returns indexes.
for(var i of arr){
console.log(i);
}Output:
10
20
30
Because it returns values.
- Loops are used for repetitive tasks.
- for loop is used when the number of iterations is known.
- while loop checks condition before execution.
- do...while executes at least once.
- Infinite loops never terminate.
- break statement exits the loop.
- for...of is used for arrays and iterable objects.
- for...in is used for objects.
Looping Statements are one of the most important concepts in JavaScript. They allow developers to execute repetitive tasks efficiently, iterate through arrays and objects, and build dynamic applications. Understanding for, while, do...while, for...of, and for...in loops is essential for mastering JavaScript programming and modern web development.