JavaScript Conditionals

Run different code based on conditions.

Conditional statements allow JavaScript programs to make decisions. They help a program choose different actions based on whether a condition is true or false.

In real life, we make decisions every day. For example:

  • If it is raining, take an umbrella.
  • If you are hungry, eat food.
  • If the traffic light is green, move forward.

JavaScript uses conditional statements in a similar way. They allow a program to evaluate conditions and execute different blocks of code depending on the result.

Why Are Conditional Statements Important?

Without conditional statements, a program would execute every line of code in the same order every time. There would be no way to react differently to different situations.

Conditional statements are commonly used for:

  • User login systems
  • Form validation
  • Age verification
  • Online shopping discounts
  • Access control and permissions

They make applications smarter and more interactive.

The if Statement

The simplest conditional statement is the if statement.

It executes a block of code only if a condition is true.

JavaScript
let age = 20;

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

Output:

JavaScript
You are eligible to vote.

The code inside the if block runs because the condition is true.

The if...else Statement

Sometimes you want one action when a condition is true and another action when it is false.

JavaScript
let age = 16;

if (age >= 18) {
    console.log("You can vote.");
} else {
    console.log("You cannot vote yet.");
}

Output:

JavaScript
You cannot vote yet.

The else block runs when the condition is false.

The if...else if...else Statement

When there are multiple conditions to check, you can use else if.

JavaScript
let score = 85;

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("Grade D");
}

Output:

JavaScript
Grade B

JavaScript checks each condition from top to bottom and executes the first matching block.

The switch Statement

The switch statement is useful when comparing a single value against multiple options.

JavaScript
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("Regular day");
}

Output:

JavaScript
Start of the week

The default block runs if none of the cases match.

Nested Conditional Statements

A conditional statement can also contain another conditional statement.

JavaScript
let isLoggedIn = true;
let isAdmin = true;

if (isLoggedIn) {
    if (isAdmin) {
        console.log("Welcome Admin");
    }
}

This is called a nested conditional statement.

Key Takeaways

  • Conditional Statements is an important topic to understand.
  • Start with small examples and practice one step at a time.
  • Use the idea in simple projects so it becomes easier to remember.