Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟨 Days 1–7 – JavaScript की नींव  ·  दिन 04

दिन 4: Loops: for, while, for…of

⏱ 80 min📶 Beginner🎯 100% self-study
दिन 4 / 30

✅ आज आप सीखेंगे

  • Loops क्यों: बिना copy-paste काम दोहराना
  • Counter वाले for loops
  • while और do…while
  • Arrays और strings के लिए for…of, objects के लिए for…in
  • break, continue, nested loops और infinite-loop bugs

🏢 असली दुनिया में

School office 40 students की attendance print करता है, दुकान bill पर 25 items जोड़ती है, coaching app 100 जवाब जाँचता है। एक ही line 100 बार लिखना संभालना असंभव है। Loop यह तीन lines में करता है – और 10 items या 10,000 के लिए एक जैसा।

for loop

JavaScript
// for (start; keep going while…; after each round)
for (let i = 1; i <= 5; i++) {
  console.log(`9 × ${i} = ${9 * i}`);
}
9 × 1 = 9 9 × 2 = 18 9 × 3 = 27 9 × 4 = 36 9 × 5 = 45
let i = 1 – शुरू में एक बारi <= 5 – हर round से पहले जाँच; false → रुकोbody चलती हैi++ – हर round के बाद, फिर दोबारा जाँच
for loop कैसे चलता है

for…of: list में चलने का सबसे आसान तरीक़ा

JavaScript
const prices = [900, 650, 700, 500];
let total = 0;
for (const price of prices) {
  total += price;
}
console.log('Total of one kg each:', total);
console.log('Average price:', total / prices.length);

for (const letter of 'Ghewar') {
  process.stdout.write(letter + ' ');
}
console.log();
Total of one kg each: 2750 Average price: 687.5 G h e w a r
Loopकब
for (let i = 0; i < n; i++)जब index या अलग step चाहिए (हर दूसरा, उल्टा)
for…of ⭐Array, string, Map या Set की हर value
for…inObject की keys (arrays के लिए नहीं)
whileRounds की संख्या पता न हो – condition बदलने तक
do…whileकम से कम एक बार चलाना (menu दिखाओ, फिर पूछो)
array.forEach / mapFunctional शैली – Day 8

while और do…while

JavaScript
// How many months to save ₹20,000 if you save ₹1,500 a month and get ₹200 bonus every 4th month?
let saved = 0, month = 0;
while (saved < 20000) {
  month++;
  saved += 1500;
  if (month % 4 === 0) saved += 200;
}
console.log(`Goal reached in ${month} months with ₹${saved}`);

let tries = 0;
do {
  tries++;
  console.log('Attempt', tries);
} while (tries < 1);   // runs once even though the condition is false afterwards
Goal reached in 13 months with ₹20100 Attempt 1
⚠️ Infinite loops page को जाम कर देते हैं

अगर condition कभी false न हो (i++ भूल गए या saved कभी नहीं बढ़ता), browser tab अटक जाता है। हमेशा जाँचें: loop के अंदर क्या बदलता है, और क्या वह आख़िरकार loop रोकेगा?

break, continue और for…in

JavaScript
const orders = [450, 0, 1200, -50, 3000, 800];
for (const amount of orders) {
  if (amount <= 0) {
    console.log('skip invalid', amount);
    continue;               // jump to the next item
  }
  if (amount > 2500) {
    console.log('big order found:', amount, '→ call the manager, stop checking');
    break;                  // leave the loop completely
  }
  console.log('ok', amount);
}

const stock = { kaju: 25, ghewar: 8, rasmalai: 0 };
for (const item in stock) {
  console.log(item, stock[item] > 0 ? 'available' : 'SOLD OUT');
}
ok 450 skip invalid 0 ok 1200 skip invalid -50 big order found: 3000 → call the manager, stop checking kaju available ghewar available rasmalai SOLD OUT

Nested loops: patterns और tables

JavaScript
for (let row = 1; row <= 4; row++) {
  let line = '';
  for (let col = 1; col <= row; col++) {
    line += '🍬';
  }
  console.log(line);
}

// attendance: 3 students × 5 days
const attendance = [
  ['Aarav', 'P', 'P', 'A', 'P', 'P'],
  ['Riya',  'P', 'P', 'P', 'P', 'P'],
  ['Kabir', 'A', 'P', 'A', 'P', 'A'],
];
for (const [name, ...days] of attendance) {
  let present = 0;
  for (const d of days) if (d === 'P') present++;
  console.log(`${name.padEnd(6)} ${present}/5  ${present >= 4 ? '✅' : '⚠️ low'}`);
}
🍬 🍬🍬 🍬🍬🍬 🍬🍬🍬🍬 Aarav 4/5 ✅ Riya 5/5 ✅ Kabir 2/5 ⚠️ low

असली task: page पर पहाड़ा दिखाना

HTML + JavaScript
<label>Table of <input id="n" type="number" value="13" style="width:70px"></label>
<button id="show">Show</button>
<ol id="list"></ol>

<script>
document.querySelector('#show').addEventListener('click', function () {
  const n = Number(document.querySelector('#n').value);
  let html = '';
  for (let i = 1; i <= 10; i++) {
    html += `<li>${n} × ${i} = <b>${n * i}</b></li>`;
  }
  document.querySelector('#list').innerHTML = html;
});
</script>
▶  A loop builds 10 list items

🏅 आज के Job Skills

✔ for और while से काम दोहराना✔ Arrays में for…of और objects में for…in✔ break और continue से loops नियंत्रित करना✔ Tables और reports के लिए nested loops

Practice tasks

🏋️ Practice – ख़ुद करें

Task 1: 2 से 20 तक सभी सम संख्याएँ और उनका योग print करें।
Solution देखें
JavaScript
let sum = 0;
const evens = [];
for (let n = 2; n <= 20; n += 2) {
  evens.push(n);
  sum += n;
}
console.log(evens.join(', '));
console.log('Sum:', sum);
2, 4, 6, 8, 10, 12, 14, 16, 18, 20 Sum: 110
Task 2: Marks [45, 78, 32, 91, 66] में से एक loop से गिनें कितने pass (≥ 33) हुए और सबसे ज़्यादा ढूँढें।
Solution देखें
JavaScript
const marks = [45, 78, 32, 91, 66];
let passed = 0, highest = -Infinity;
for (const m of marks) {
  if (m >= 33) passed++;
  if (m > highest) highest = m;
}
console.log(`Passed: ${passed}/${marks.length}, highest: ${highest}`);
Passed: 4/5, highest: 91
Task 3: Nested loops से 5 rows वाला * का pyramid print करें (spaces से बीच में)।

Quick quiz

🧠 Quick Quiz – ख़ुद को जाँचें

1. for (let i = 0; i < 5; i++) कितनी बार चलता है?
0,1,2,3,4।
2. Array की हर value के लिए सबसे अच्छा loop?
for…in keys देता है।
3. continue…
break रोकता है।
4. do…while loop कम से कम चलता है…
जाँच बाद में।
5. आपके loop के बाद page जाम। सबसे संभावित?
Infinite loop।

Interview corner

💼 Interview Corner

for…in बनाम for…of?

for…in object की enumerable property keys (strings) पर चलता है, inherited समेत – objects के लिए। for…of iterables (arrays, strings, Map, Set) की values पर। Arrays के लिए for…of।

forEach loop जल्दी कैसे रोकेंगे?

forEach से break नहीं कर सकते (throw के अलावा)। break वाला for…of लें, या जल्दी रुकने वाले methods जैसे some(), every() या find()।

सारांश

  • गिनती वाली पुनरावृत्ति के लिए for (start; condition; step)।
  • Array values के लिए for…of, object keys के लिए for…in।
  • अज्ञात गिनती के लिए while; condition बदलती रहे।
  • break रोकता है, continue छोड़ता है; nested loops tables बनाते हैं।
➡️ आगे

Day 5: functions – logic को दोबारा उपयोगी टुकड़ों में: GST, EMI और discount calculators।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

कौन सा loop सबसे तेज़?
रोज़ के app code में फ़र्क नगण्य है; सबसे पढ़ने लायक loop चुनें। मापने के बाद ही optimise करें।
process.stdout.write क्या है?
बिना नई line के print करने का Node.js तरीक़ा, यहाँ सिर्फ़ demo के लिए। Browsers में console.log लें या पहले string बनाएँ।
← JavaScript (30 Days)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicदिन 5: Functions, Arrow Functions और Scope आपकी प्रगति: 4 / 30 (13%) · Lesson 5 / 30 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें