Control Structures in JavaScript

By Shakib Ansari | Date: Sun, Oct 19, 2025

JavaScript mein control structures ka use code ke flow ko control karne ke liye hota hai. Iska matlab kaunsa code kab execute hoga, ye decide karte hain ye structures. Ye programming ke decision-making aur repetition ke tools hain.

Is article mein hum seekhenge:

  1. Conditional statements (if, else, else if, switch)
  2. Loops (for, while, do-while, for...of, for...in)
  3. Break aur continue statements

3.1 Conditional Statements (if, else, else if, switch)

Conditional statements ka use decision lene ke liye hota hai. JavaScript condition ko check karti hai aur uske basis par code execute karta hai.

if Statement

if only tab run hota hai jab condition true hoti hai.

let age = 18;
if (age >= 18) {
  console.log("You can vote!");
}

Output: You can vote!

if...else Statement

Agar condition false hoti hai, toh else part execute hota hai.

let isRaining = false;

if (isRaining) {
  console.log("Take an umbrella!");
} else {
  console.log("Enjoy the sunshine!");
}

Output: Enjoy the sunshine!

else if Statement

Agar multiple conditions check karni ho, toh else if use karte hain.

let marks = 85;

if (marks >= 90) {
  console.log("Grade A");
} else if (marks >= 75) {
  console.log("Grade B");
} else {
  console.log("Grade C");
}

Output: Grade B

switch Statement

Jab ek variable se multiple values ko compare karni ho, toh switch statements ka use karte hai, kaafi clean code deta hai.

let day = "Monday";

switch (day) {
  case "Monday":
    console.log("Start of the week!");
    break;
  case "Friday":
    console.log("Weekend is near!");
    break;
  default:
    console.log("Just another day!");
}

Output: Start of the week!

Note: Har case ke baad break likhna important hai warna next case bhi execute ho jaayega.

3.2 Loops (for, while, do-while, for...of, for...in)

Loops ka use tab hota hai jab same code ko bar-bar execute karna ho — jaise 1 se 10 tak numbers print karna.

for Loop

for loop ka use jab hota hai, jabo iteration count pehle se hi pata ho. Matlab ki ye pata hona ki loop ko kitni baar run karna hai.

for (let i = 1; i <= 5; i++) {
  console.log("Count:", i);
}

Output:

Count: 1  
Count: 2  
Count: 3  
Count: 4  
Count: 5

while Loop

while loop ka use jab hota hai, jab condition ke based loop chalana ho.

let i = 1;
while (i <= 3) {
  console.log("Number:", i);
  i++;
}

Output:

Number: 1  
Number: 2  
Number: 3

do...while Loop

do...while loop at least ek baar to zarur run hota hai — chahe condition false hi kyu na ho.

let j = 5;

do {
  console.log("Value:", j);
  j++;
} while (j < 5);

Output:

Value: 5

(Condition false thi, par ek baar code execute hua.)

for...of Loop

Array ke elements par loop lagane ke liye best hai.

let fruits = ["Apple", "Banana", "Cherry"];

for (let fruit of fruits) {
  console.log(fruit);
}

Output:

Apple  
Banana  
Cherry

for...in Loop

Object ki properties par iterate karne ke liye use hota hai.

let person = { name: "Nitin", age: 21, city: "Delhi" };

for (let key in person) {
  console.log(key + ":", person[key]);
}

Output:

name: Nitin  
age: 21  
city: Delhi

3.3 Break and Continue Statements

Ye dono loop ke flow ko control karne ke liye use hote hain.

break Statement

Loop ko immediately stop kar deta hai.

for (let i = 1; i <= 5; i++) {
  if (i === 3) break;
  console.log(i);
}

Output:

1  
2

(Jab i = 3 hua, loop ruk gaya.)

continue Statement

Current iteration skip karke next iteration start karta hai.

for (let i = 1; i <= 5; i++) {
  if (i === 3) continue;
  console.log(i);
}

Output:

1  
2  
4  
5

(3 ko skip ho gaya.)

Conclusion

Programming mein Control Structures bahut important hote hain.

Ye decide karte hain:

  • Kya code chalega (conditional statements)
  • Kitni baar chalega (loops)
  • Aur kab rukega (break/continue)
About the Author

Hi, I'm Shakib Ansari, Founder and CEO of BeyondMan. I'm a highly adaptive developer who quickly learns new programming languages and delivers innovative solutions with passion and precision.

Web-Development

Comments