JavaScript Loops Kya Hote Hain ?
JavaScript me loops ka use kisi bhi task ko repeat karne ke liye kiya jata hai. Agar hume ek hi code ko multiple times execute karna ho, to loops ka use bahut helpful hota hai. JavaScript me different types ke loops hote hain jo alag-alag situations ke liye use kiye jate hain.
Chaliye ek-ek karke sabhi loops ko samajhte hain:
1. For Loop :
For loop ek control flow statement hai jo ek block of code ko ek fixed number of times execute karne ke liye use hota hai. Yeh loop initialization, condition, aur increment/decrement ke sath kaam karta hai. Jab tak condition true hoti hai, loop execute hota rehta hai. Yeh loop tab use hota hai jab hume kisi task ko repeat karna ho, jaise ki array traverse karna ya numbers print karna. loop ka syntax hoti hai:
for(initialization; condition; update) {
// Code block
}
Example:
for(let i = 1; i <= 5; i++) {
console.log("Number: " + i);
}
Explanation :
let i = 1; → Yaha i ka initial value 1 set kiya gaya hai.
i <= 5; → Jab tak i ki value 5 se chhoti ya barabar hai, loop chalta rahega.
i++ → Har baar loop ke complete hone par i ki value 1 se badhti rahegi.
2. While Loop :
While loop ek aisa loop hai jo ek condition ke true hone tak continuously execute hota rahta hai. Jab tak condition true hai, tab tak loop ke andar ka code run hota rahega. Jaise hi condition false hoti hai, loop terminate ho jata hai. Ye loop tab useful hota hai jab hume pehle se nahi pata hota ki kitni baar loop chalana hai. Isme condition har iteration ke baad check hoti hai. Agar condition kabhi false na ho, to infinite loop ban sakta hai.
syntax:
while(condition) {
// Code to be executed
}
Example:
let i = 1;
while(i <= 5) {
console.log("Count: " + i);
i++;
}
Explanation:
Jab tak i <= 5 condition true rahegi, loop chalega.
i++ har baar loop complete hone ke baad i ko badhata hai.
3. Do-While Loop :
Do-While Loop ek control flow statement hai jo ek block of code ko execute karta hai aur phir condition check karta hai. Yeh loop kam se kam ek baar zaroor execute hota hai, chahe condition false ho ya na ho. Syntax mein do block pehle likha jata hai, phir while statement ke andar condition di jati hai. Jab tak condition true hoti hai, loop repeat hota hai. Yeh loop tab useful hota hai jab pehle execution zaroori ho, jaise user input validation ya menu-based programs mein.
Syntax :
do {
// Code to be executed
} while(condition);
Example:
let i = 1;
do {
console.log("Value: " + i);
i++;
} while(i <= 5);
Explanation:
Pehle console.log(“Value: ” + i); execute hoga.
Phir condition check hogi (i <= 5).
Agar condition true hai to loop firse chalega, nahi to terminate ho jayega.
4. For-In Loop :
For-In Loop ek aisa loop hai jo kisi bhi iterable object (arrays, strings, objects) ke elements ko ek-ek karke iterate karta hai. Yeh loop har iteration me ek value ko assign karta hai aur automatically agle element par move hota hai. JavaScript me, arrays ke elements ya objects ke properties ko access karne ke liye use hota hai.
Syntax :
for(let key in object) {
// Code to be executed
}
Example:
let student = {name: "Rahul", age: 20, city: "Delhi"};
for(let key in student) {
console.log(key + ": " + student[key]);
}
Explanation:
key object ke andar ki har property ko represent karta hai.
student[key] property ka value return karta hai.
Output:
name: Rahul
age: 20
city: Delhi
5. For-Of Loop :
JavaScript ka for-of loop ek aasan tareeka hai iterable objects (arrays, strings, maps, sets) ke elements ko ek-ek karke access karne ka. Yeh loop index ki zaroorat nahi padne deta, seedha values ko iterate karta hai.
Syntax:
for(let value of iterable) {
// Code to be executed
}
Example:
let numbers = [10, 20, 30, 40];
for(let num of numbers) {
console.log(num);
}
Explanation :
num array ke har element ko represent karega.
Loop har iteration me ek value ko print karega.
Output:
10
20
30
40
6. ForEach Loop :
ForEach loop ek special loop hai jo arrays ya collections ke har element ko iterate karne ke liye use hota hai. Ye traditional loops (like for ya while) se alag hota hai kyunki isme hume index ya counter manage karne ki zaroorat nahi hoti. ForEach loop automatically har element ko access karta hai aur uspar operations perform karta hai. Yeh readable aur concise hota hai, jo code ko clean aur easy to understand banata hai. Yeh mostly programming languages jaise JavaScript, Java, PHP, C# mein use hota hai, jisme objects ya arrays ke elements par efficiently loop lagaya jata hai.
Syntax :
array.forEach(function(element, index, array) {
// Code to be executed
});
Example:
let fruits = ["Apple", "Banana", "Mango"];
fruits.forEach(function(fruit, index) {
console.log(index + ": " + fruit);
});
Explanation :
fruit array ke elements ko represent karta hai.
index har element ka position show karta hai.
Output :
0: Apple
1: Banana
2: Mango
Conclusion
Loop Type | Use Case | Best for |
---|---|---|
For Loop | Jab fixed iterations pata ho | Arrays, counting loops |
While Loop | Jab condition satisfy hone tak loop chalana ho | Event-based looping |
Do-While Loop | Jab at least ek baar execute karna ho | Menu-driven programs |
For-In Loop | Objects ki properties ko iterate karne ke liye | Objects |
For-Of Loop | Arrays aur iterables ke liye | Arrays, strings |
ForEach Loop | Arrays par functions apply karne ke liye | Arrays |
Quiz: Test Your Knowledge on JavaScript Loops
Bonus: Practical Application!
Aaj hi apne webpage par JavaScript loops ka istemal karke dekhein!
JavaScript loops ko sahi tareeke se samajhne ke liye different types jaise for, while, do-while, forEach ka upayog karein aur apne code ko aur bhi efficient banayein.