JS फॉर लूप
In this page:
for (let i = start; i < end; i++) {
// loop body
}
for (const item of iterable) {
// loop body
}
Standard for Loop
एक standard for loop के parentheses के अंदर तीन हिस्से होते हैं, एक initializer जो शुरुआत में एक बार चलता है, एक condition जो हर iteration से पहले जांची जाती है, और एक increment step जो हर iteration के बाद चलता है।
उदाहरण: The Standard for Loop
// Classic for-loop: `let i = 0; i < 3; i++`
// Classic for-loop: `let i = 0; i < 3; i++`
for (let i = 0; i < 3; i++) {
// Print `i` to the console
// Print `i` to the console
console.log(i);
}
for...in
for...in किसी object की enumerable property keys पर iterate करता है, आपको बारी-बारी हर key name देते हुए, जिसका इस्तेमाल आप संबंधित value ढूंढने के लिए कर सकते हैं।
इसे आमतौर पर arrays के लिए टाला जाता है क्योंकि यह keys को strings के रूप में iterate करता है और inherited properties भी पकड़ सकता है।
उदाहरण: for...in
// Declare the constant `user`, set to `{ name: "Sam", age: 30 }`
// Declare the constant `user`, set to `{ name: "Sam", age: 30 }`
const user = { name: "Sam", age: 30 };
// Loop over the enumerable keys of `user`, binding each to `key`
// Loop over the enumerable keys of `user`, binding each to `key`
for (let key in user) {
// Print `key, user[key]` to the console
// Print `key, user[key]` to the console
console.log(key, user[key]);
}
for...of
for...of सीधे किसी भी iterable की values पर iterate करता है, जैसे arrays, strings, और Sets और Maps जैसी ज़्यादा advanced structures, बिना किसी index को manage किए।
उदाहरण: for...of
// Declare the constant `colors`, set to `["red", "green", "blue"]`
// Declare the constant `colors`, set to `["red", "green", "blue"]`
const colors = ["red", "green", "blue"];
// Loop over `colors`, binding each item to `color`
// Loop over `colors`, binding each item to `color`
for (let color of colors) {
// Print `color` to the console
// Print `color` to the console
console.log(color);
}
break और continue
break पूरे loop से तुरंत बाहर निकल जाता है, बाकी बची सभी iterations को skip करते हुए, जबकि continue सिर्फ current iteration को skip करता है और अगली पर बढ़ जाता है, बिना loop को खत्म किए।
उदाहरण: break and continue
// Classic for-loop: `let i = 0; i < 5; i++`
// Classic for-loop: `let i = 0; i < 5; i++`
for (let i = 0; i < 5; i++) {
if (i === 3) break;
if (i === 1) continue;
// Print `i` to the console
// Print `i` to the console
console.log(i);
}
Nested Loops
एक loop को दूसरे loop के अंदर रखा जा सकता है, जो outer loop की हर एक iteration के लिए inner loop को पूरी तरह चलाता है, यह आमतौर पर grid जैसी या two-dimensional data पर काम करने के लिए इस्तेमाल होता है।
उदाहरण: Nested Loops
// Classic for-loop: `let i = 1; i <= 2; i++`
// Classic for-loop: `let i = 1; i <= 2; i++`
for (let i = 1; i <= 2; i++) {
// Classic for-loop: `let j = 1; j <= 2; j++`
// Classic for-loop: `let j = 1; j <= 2; j++`
for (let j = 1; j <= 2; j++) {
// Print `i, j` to the console
// Print `i, j` to the console
console.log(i, j);
}
}
- गलत stopping condition लिखना, जैसे zero-based array length के साथ < के बजाय <= इस्तेमाल करना, जिससे out-of-bounds access होता है।
- loop counter को increment करना भूल जाना, जो एक infinite loop बनाता है जो अपनी stopping condition कभी पूरी नहीं करता।
- किसी array पर for...in इस्तेमाल करना जब for...of या एक regular for loop असल में ज़्यादा उपयुक्त, index-safe choice है।
- एक standard for loop एक initializer, एक condition, और एक increment step के आधार पर चलता है, सब उसके parentheses में defined।
- for...in किसी object की property keys पर iterate करता है, जबकि for...of सीधे किसी iterable की values पर iterate करता है।
- break पूरी तरह loop से बाहर निकलता है, continue अगली iteration पर skip करता है, और loops एक-दूसरे के अंदर nested हो सकते हैं।
for, for...in, और for...of loops सभी core JavaScript features हैं जो हर modern browser में समान रूप से supported हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic