Advertisement

Learn Control Structures in JavaScript in Hindi – Easy Guide for Beginners

JavaScript में Control Structures प्रोग्राम को कंट्रोल करने का आसान तरीका हैं। यह ब्लॉग Conditionals (if, else if, else, switch, ternary operator) और Loops (for, while, do-while) को सरल भाषा में समझाता है। साथ ही, break, continue, nested loops, प्रैक्टिकल एक्सरसाइज (मल्टीप्लिकेशन टेबल और प्राइम नंबर चेक), 5 MCQ क्विज़, और मिनी प्रोजेक्ट (नंबर गेसिंग गेम) आपके JavaScript स्किल्स को बढ़ाएंगे। यह गाइड हिंदी में है, टेक्निकल टर्म्स (जैसे if, for) English में हैं, और कोड पूरी तरह English में है।

What is Control Structures in JavaScript with example in Hindi (JavaScript में कंट्रोल स्ट्रक्चर क्या होते हैं, उदाहरण सहित)

Control Structures प्रोग्राम को यह बताते हैं कि कब क्या करना है। Conditionals डिसीजन लेते हैं, जैसे यूज़र की उम्र चेक करना। Loops कोड को बार-बार चलाते हैं, जैसे नंबर लिस्ट प्रिंट करना। JavaScript में कंडीशन्स Truthy (जैसे 1, "hello") या Falsy (0, "", null) में बदलती हैं। गलत कंडीशन्स से बग्स हो सकते हैं। बेस्ट प्रैक्टिस: सिम्पल कोड, let यूज़ करें। यूज़ केस: यूज़र लॉगिन, डेटा प्रोसेसिंग। पिटफॉल्स: गलत लॉजिक, इनफिनिट लूप्स।
let isLoggedIn = true;
if (isLoggedIn) {
  console.log("Welcome to the app!");
} else {
  console.log("Please log in.");
}

Types of conditional statements in JavaScript with examples in Hindi

Conditionals प्रोग्राम को कंडीशन्स के आधार पर डिसीजन लेने में मदद करते हैं। ये चेक करते हैं कि कंडीशन Truthy है या Falsy, और उसी हिसाब से कोड चलता है।

if: Checking a condition

if एक कंडीशन चेक करता है। अगर कंडीशन Truthy, तो कोड ब्लॉक चलता है, वरना स्किप। let स्कोप को सिमिट करता है। गलत कंडीशन से बग्स हो सकते हैं। बेस्ट प्रैक्टिस: सिम्पल कंडीशन्स, === यूज़ करें। यूज़ केस: यूज़र वैलिडेशन। पिटफॉल्स: गलत ऑपरेटर या अनडिफाइंड वैरिएबल्स।
let age = 18;
if (age >= 18) {
  console.log("You can vote!");
}

else if: Checking multiple Conditions

else if कई कंडीशन्स चेक करता है। पहली Truthy कंडीशन का ब्लॉक चलता है। एक के बाद एक चेक होता है। गलत ऑर्डर से बग्स। बेस्ट प्रैक्टिस: कम else if, स्पेसिफिक कंडीशन पहले। यूज़ केस: ग्रेडिंग सिस्टम। पिटफॉल्स: ज्यादा नेस्टिंग।
let score = 85;
if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 80) {
  console.log("Grade: B");
}

else: Default Condition

else तब चलता है जब कोई कंडीशन Truthy नहीं। डिफॉल्ट आउटपुट देता है। कोई चेकिंग नहीं, फास्ट। बेस्ट प्रैक्टिस: डिफॉल्ट मेसेज। यूज़ केस: एरर हैंडलिंग। पिटफॉल्स: अनावश्यक else
let temperature = 15;
if (temperature > 30) {
  console.log("It's hot!");
} else {
  console.log("It's cool!");
}

switch: Checking multiple values

switch एक वैल्यू को कई केस से === के साथ चेक करता है। मैचिंग केस चलता है। break न होने पर फॉल-थ्रू। default अनमैच्ड केस हैंडल करता है। बेस्ट प्रैक्टिस: break, default यूज़ करें। यूज़ केस: डे ऑफ वीक। पिटफॉल्स: फॉल-थ्रू, गलत टाइप।
let day = 2;
switch (day) {
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  default:
    console.log("Invalid day");
}

Ternary Operator: Short Conditional

Ternary Operator (condition ? expr1 : expr2) सिंगल-लाइन कंडीशनल है। Truthy पर पहला, Falsy पर दूसरा एक्सप्रेशन। फास्ट, लेकिन नेस्टिंग अनरीडेबल। बेस्ट प्रैक्टिस: सिम्पल यूज़। यूज़ केस: वैरिएबल असाइनमेंट। पिटफॉल्स: नेस्टेड टर्नरी।
let age = 20;
let isAdult = age >= 18 ? "Yes" : "No";
console.log(isAdult); // Yes

Types of Loops in JavaScript with examples in Hindi

Loops कोड को रिपीट करते हैं। for फिक्स्ड इटरेशन्स, while डायनामिक, do-while कम से कम एक रन। गलत कंडीशन्स से इनफिनिट लूप्स। बेस्ट प्रैक्टिस: let, सिम्पल लॉजिक। यूज़ केस: डेटा प्रोसेसिंग। पिटफॉल्स: गलत इन्क्रिमेंट।

for: Fixed Iterations

for लूप एक counter-controlled iteration construct है जो कोड को एक निर्धारित संख्या में बार चलाने के लिए प्रयोग होता है। यह initialization → condition check → increment/decrement के सीक्वेंस पर आधारित होता है। कम्पाइलर इसे predictable iteration pattern के कारण highly optimized करता है।
for (let i = 1; i <= 5; i++) {
  console.log(i); // 1, 2, 3, 4, 5
}

while: Dynamic Iterations

while लूप एक entry-controlled iteration है जो तब तक चलता है जब तक दी गई boolean expression truthy रहती है। यह उन परिस्थितियों के लिए उपयुक्त है जहाँ iteration count uncertain या dynamic होता है। यह अधिक runtime-driven control देता है।
let i = 1;
while (i <= 5) {
  console.log(i);
  i++;
}

do-while: At least one run

do-while एक post-tested loop construct है जो body को कम से कम एक बार execute करता है, भले ही condition false हो। यह mandatory first-run logic वाले scenarios में प्रयोग किया जाता है।
let i = 1;
do {
  console.log(i);
  i++;
} while (i <= 5);

Break: Stop Loop

break statement एक control transfer keyword है जो लूप के normal flow को तुरंत समाप्त कर देता है। यह nested loops और conditional exits में execution efficiency बढ़ाने में मदद करता है।
for (let i = 1; i <= 5; i++) {
  if (i === 3) break;
  console.log(i); // 1, 2
}

Continue: Skipping an Iteration

continue statement लूप की current iteration को skip कर अगली iteration पर jump करता है। यह conditional skipping में उपयोगी होता है जहाँ कुछ values या cases को process नहीं करना हो।
for (let i = 1; i <= 5; i++) {
  if (i === 3) continue;
  console.log(i); // 1, 2, 4, 5
}

Nested Loops: Multi-Level Interaction

Nested loop यानी loop within a loop, complex data structures या multi-dimensional arrays पर काम करने के लिए उपयोगी होते हैं। हर inner loop outer loop की हर iteration के लिए execute होता है, जिससे time complexity बढ़ती है — अक्सर O(n²) या अधिक।
for (let i = 1; i <= 3; i++) {
  for (let j = 1; j <= 2; j++) {
    console.log(`i=${i}, j=${j}`);
  }
}

Exercise: Multiplication Table & Primary Number Check

Task 1: 1-10 का नंबर लेकर मल्टीप्लिकेशन टेबल प्रिंट करें। Task 2: नंबर चेक करें कि प्राइम है या नहीं। बेस्ट प्रैक्टिस: वैलिडेशन। यूज़ केस: मैथ ऐप्स।
let num = parseInt(prompt("Enter a number for multiplication table (1-10):"));
if (num >= 1 && num <= 10) {
  for (let i = 1; i <= 10; i++) {
    console.log(`${num} x ${i} = ${num * i}`);
  }
} else {
  console.log("Please enter a number between 1 and 10");
}

let number = parseInt(prompt("Enter a number to check if it's prime:"));
let isPrime = true;
if (number <= 1) {
  isPrime = false;
} else {
  for (let i = 2; i <= Math.sqrt(number); i++) {
    if (number % i === 0) {
      isPrime = false;
      break;
    }
  }
}
console.log(number + (isPrime ? " is prime" : " is not prime"));

Quiz: Loop & Conditional Logic

  1. What is the output of if (x > 10) { console.log("Big"); } else { console.log("Small"); } when x = 5?

    • A) Big

    • B) Small

      Advertisement
    • C) Error

    • D) Undefined Answer: B) Small

  2. What is the output of for (let i = 0; i < 3; i++) { console.log(i); }?

    Advertisement
    • A) 1, 2, 3

    • B) 0, 1, 2

    • C) 0, 1, 2, 3

    • D) Infinite loop Answer: B) 0, 1, 2

  3. What is the output of switch (color) { case "red": console.log("Stop"); break; default: console.log("Go"); } when color = "green"?

    • A) Stop

    • B) Go

    • C) Error

    • D) Nothing Answer: B) Go

  4. What is the output of let i = 0; while (i < 3) { console.log(i); i++; }?

    • A) 0, 1, 2

    • B) 1, 2, 3

    • C) Infinite

    • D) 0, 1 Answer: A) 0, 1, 2

  5. What is the output of for (let i = 1; i <= 5; i++) { if (i === 3) continue; console.log(i); }?

    • A) 1, 2, 3, 4, 5

    • B) 1, 2, 4, 5

    • C) 3

    • D) 1, 2 Answer: B) 1, 2, 4, 5

Mini Project: नंबर गेसिंग गेम

Task: 1-100 के बीच रैंडम नंबर जनरेट करें, यूज़र गेस करे। हिन्ट्स (Too high/low) और गेस काउंट। do-while यूज़। बेस्ट प्रैक्टिस: वैलिडेशन। यूज़ केस: गेम्स।
let randomNumber = Math.floor(Math.random() * 100) + 1;
let guessCount = 0;
let userGuess;

do {
  userGuess = parseInt(prompt("Guess a number between 1 and 100:"));
  guessCount++;
  
  if (isNaN(userGuess) || userGuess < 1 || userGuess > 100) {
    console.log("Please enter a valid number between 1 and 100");
  } else if (userGuess > randomNumber) {
    console.log("Too high! Try again.");
  } else if (userGuess < randomNumber) {
    console.log("Too low! Try again.");
  } else {
    console.log(`Congratulations! You guessed the number ${randomNumber} in ${guessCount} attempts!`);
  }
} while (userGuess !== randomNumber);

Conclusion

Control Structures JavaScript में प्रोग्राम को स्मार्ट बनाते हैं। Conditionals और Loops से डिसीजन और रिपीट टास्क्स आसान हो जाते हैं। प्रैक्टिस करें और अगले मॉड्यूल में फंक्शन्स सीखें।

Also Read: 

Table of Contents

Close

Comments

Share to other apps

Report Content

Why are you reporting this content?

Your selection helps us review the content and take appropriate action.

Hate & Discrimination
Content that spreads hate or unfair treatment against a person or group because of who they are.
Abuse & Harassment
Content that insults, threatens, bullies, or makes someone uncomfortable.
Violence & Threats
Content that talks about hurting people, animals, or property, or supports violence.
Child Safety
Any content that harms, exploits, or puts children at risk.
Privacy Violation
Sharing someone’s personal information or photos without permission.
Illegal & Regulated Activities
Content that promotes or helps with illegal activities like drugs, weapons, or trafficking.
Spam & Misleading Content
Fake, misleading, or repeated content meant to trick users.
Suicide or Self-Harm
Content that encourages or explains self-harm or suicide.
Sensitive or Disturbing Content
Shocking or graphic content that may upset users.
Impersonation
Pretending to be another person or organization.
Extremism & Hate Groups
Content that supports violent groups or hateful ideas.
Civic Integrity
Content that spreads false information about elections or public processes.