Facebook Pixel

Conditional Statements

In programming, conditional statements allow your code to make decisions — just like we (humans) do. They let you execute certain parts of code only when specific conditions are met.

In JavaScript, the main conditional structures are:

  • if and else
  • if...else if...else (also called if-else ladder)
  • switch statement

Let’s explore each of them step by step.

The if Statement

The if statement is used to run a block of code only if a condition is true.

Syntax:

if (condition) {
  // code to run if condition is true
}

Example:

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

Explanation: Here, the condition age >= 18 is true, so the message "You are eligible to vote." will be printed. If it were false, the code inside the if block would simply be skipped.

The if...else Statement

Sometimes you want to run one block of code if the condition is true, and another block if it’s false. That’s where else comes in.

Syntax:

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

Example:

let temperature = 25;
 
if (temperature > 30) {
  console.log("It’s a hot day!");
} else {
  console.log("The weather is pleasant.");
}

Output:

The weather is pleasant.

Explanation: Since temperature > 30 is false, the else block executes instead.

The if...else if...else Ladder

When you have multiple conditions to check, using just if and else becomes messy. That’s where the if-else ladder (also called a chain) helps — you can check several conditions in sequence.

Syntax:

if (condition1) {
  // code if condition1 is true
} else if (condition2) {
  // code if condition1 is false and condition2 is true
} else if (condition3) {
  // code if the above are false but this is true
} else {
  // code if none of the conditions are true
}

Example:

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

Output:

Grade: A

Explanation: The program checks each condition in order. Since marks >= 75 is true, it prints "Grade: A" and skips the rest.

The switch Statement

If you’re comparing a single value against multiple possible options, the switch statement is a cleaner alternative to multiple if...else if conditions.

Syntax:

switch (expression) {
  case value1:
    // code if expression === value1
    break;
  case value2:
    // code if expression === value2
    break;
  default:
    // code if none of the above cases match
}

Example:

let day = "Tuesday";
 
switch (day) {
  case "Monday":
    console.log("Start of the week!");
    break;
  case "Tuesday":
    console.log("Keep going, it’s only Tuesday!");
    break;
  case "Friday":
    console.log("Finally, Friday!");
    break;
  default:
    console.log("Just another day...");
}

Output:

Keep going, it’s only Tuesday!

Explanation: Here, JavaScript checks which case matches the value of day. When it finds "Tuesday", it runs that block and stops at break. Without the break, it would continue running the next cases too (a behavior called “fall-through”).

Quick Recap -

Statement Type Description Example Use Case
if Runs a block if condition is true Check if user is logged in
if...else Runs one block if true, another if false Check if a number is even or odd
if...else if...else Checks multiple conditions in sequence Grading system or age categories
switch Compares one value against multiple options Day names, menu choices, modes