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.
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote.");
}Output:
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.
let age = 16;
if (age >= 18) {
console.log("You can vote.");
} else {
console.log("You cannot vote yet.");
}Output:
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.
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:
Grade BJavaScript 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.
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:
Start of the weekThe 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.
let orderTotal = 1500;
if (orderTotal > 1000) {
console.log("Free Shipping Available");
} else {
console.log("Shipping Charges Apply");
}Output:
Free Shipping AvailableThis 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.
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.