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.

Real-World Example

Imagine an online shopping website that offers free shipping for orders above 1000.

javascript
let orderTotal = 1500;

if (orderTotal > 1000) {
    console.log("Free Shipping Available");
} else {
    console.log("Shipping Charges Apply");
}

Output:

javascript
Free Shipping Available

This helps the website provide different experiences based on the customer's order value.

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.

Summary

Conditional statements allow JavaScript programs to make decisions based on different conditions. The if, if...else, if...else if, and switch statements help control the flow of a program. They are widely used in real-world applications such as login systems, shopping websites, and form validation. Understanding conditional statements is an important step toward writing smarter and more interactive JavaScript programs.

Let's learn with DevBrainBox AI