A Complete Guide to JavaScript Conditional Statements

A Complete Guide to JavaScript Conditional Statements

Understanding Conditional Statements in JavaScript

Conditional statements are one of the core concepts in JavaScript, allowing you to execute specific blocks of code based on given conditions. Whether you're checking someone's eligibility to vote or deciding between multiple options, conditional statements help control the flow of your program.

  • if-else

  • nested if-else

  • switch


Types of Conditional Statements

  1. if-else
    The simplest form of a conditional statement, used when you need to execute one block of code if a condition is true and another block if it's false.

  2. nested if-else
    A variation of if-else statements where one if or else block contains another if-else. This is used for more complex conditions.

  3. switch
    A cleaner way to handle multiple conditions, often used when comparing a variable against multiple values.


Conditional Logic Example

Let’s consider a real-world example:
To apply for a voter ID card, your age must be greater than 18. This condition is checked using conditional statements.

javascriptCopyEditlet age = 20;

if (age > 18) {
  console.log("You are eligible to apply for a voter ID card.");
} else {
  console.log("You are not eligible to apply for a voter ID card.");
}

Examples of Conditional Statements in Action

Example 1: Checking Eligibility to Vote

Question: Check if a person aged 16 is eligible to vote.

javascriptCopyEditlet age = 16;

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

Output:
You are not eligible to vote.


Example 2: Checking Eligibility for a 30-Year-Old

Question: Check if a person aged 30 is eligible to vote.

javascriptCopyEditlet age = 30;

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

Output:
You are eligible to vote.

Eligibility Check 2


Nested if-else Example

Scenario: A company offers different discounts based on age groups.

javascriptCopyEditlet age = 25;

if (age < 18) {
  console.log("You are eligible for a 10% discount.");
} else if (age >= 18 && age <= 25) {
  console.log("You are eligible for a 15% discount.");
} else {
  console.log("You are eligible for a 20% discount.");
}

Output:
You are eligible for a 15% discount.


Using switch for Multiple Cases

Scenario: Determine the grade based on marks.

javascriptCopyEditlet grade = "B";

switch (grade) {
  case "A":
    console.log("Excellent!");
    break;
  case "B":
    console.log("Good job!");
    break;
  case "C":
    console.log("You can do better.");
    break;
  default:
    console.log("Invalid grade.");
}

Output:
Good job!


Key Points to Remember

  1. if-else is for simple decisions like checking eligibility or basic conditions.

  2. Nested if-else handles complex logic with multiple levels of conditions.

  3. switch is cleaner for multi-value checks, such as menu options or grading systems.

  4. Always use logical operators (e.g., &&, ||, !) to combine conditions effectively.