🔀 Conditional Statements

Last Updated: 12th October 2025


Conditional statements help your program make decisions based on conditions (true/false).
They allow different parts of code to run depending on logic.

Hinglish Tip 🗣: Conditional statements ka kaam hota hai — “agar ye condition sahi hai to ye karo, warna kuch aur karo”.


1. if Statement

Used when you want to run a block of code only if a condition is true.

Syntax:

if (condition) {
  // code runs only if condition is true
}

Example

let age = 20;

if (age >= 18) {
  console.log("You are eligible to vote.");
}

💡 Here, the message prints only if age is 18 or above.


2. if...else Statement

Used when you have two choices — if the condition is true, do something; otherwise, do something else.

Syntax:

if (condition) {
  // code runs if condition is true
} else {
  // code runs if condition is false
}

Example

let marks = 35;

if (marks >= 40) {
  console.log("Pass");
} else {
  console.log("Fail");
}

Hinglish Tip 🗣: “Agar ye nahi to wo” — yahi if...else ka simple rule hai.


3. else if Statement

Used when you have multiple conditions to check.

Syntax:

if (condition1) {
  // code runs if condition1 is true
} else if (condition2) {
  // code runs if condition1 is false and condition2 is true
} else {
  // code runs if both condition1 and condition2 are false
}

Example

let score = 75;

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

Hinglish Tip 🗣: Ye ladder jaisa hota hai — har “else if” ek step hai, upar se neeche tak condition check hoti hai.


4. Nested if Statement

When an if is inside another if, it’s called a nested if.

Syntax:

if (condition1) {
  if (condition2) {
    // code runs if both condition1 and condition2 are true
  }
}

Example

let username = "admin";
let password = "1234";

if (username === "admin") {
  if (password === "1234") {
    console.log("Login successful");
  } else {
    console.log("Incorrect password");
  }
} else {
  console.log("Invalid username");
}

💡 Useful when you need to check multiple related conditions.


5. switch Statements

Used when you need to compare one value against many options — cleaner and faster than multiple if...else if.

Syntax:

switch (expression) {
  case value1:
    // code block
    break;
  case value2:
    // code block
    break;
  default:
    // code if no match found
}

Example

let day = 3;

switch (day) {
  case 1:
    console.log("Monday");
    break;
  case 2:
    console.log("Tuesday");
    break;
  case 3:
    console.log("Wednesday");
    break;
  default:
    console.log("Invalid day");
}

Hinglish Tip 🗣: Switch ko samjho jaise “menu card” — ek value ke hisaab se specific case run hota hai.


💡 Quick Practice

  1. Write a program that checks if a number is positive, negative, or zero.
  2. Write a program that prints the day name based on number (1–7) using switch.
  3. Check if a person is adult (18+) or minor using if...else.
  4. Use nested if to validate username and password.
  5. Exercises Set 1
  6. Exercises Set 2