JavaScript Conditional Statements: if, else, and switch Explained
When you start learning JavaScript, one of the most important concepts you must understand is how to make decisions in your code. Conditional statements allow your program to execute different blocks of code depending on specific conditions.
In this tutorial, we’ll explore JavaScript conditional statements — including if, else if, else, and switch — with easy examples and practical explanations. Let’s make your logic smarter 💪
What are Conditional Statements in JavaScript?
Conditional statements help your code make decisions. For example, “if the user is logged in, show the dashboard, otherwise show the login page.”
They allow you to control the flow of execution based on true or false conditions.
1️⃣ The if Statement
The if statement is used to check a condition. If it’s true, the code inside the block runs.
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote!");
}
✅ Output: You are eligible to vote!
2️⃣ if...else Statement
The else block runs only if the if condition is false.
let age = 16;
if (age >= 18) {
console.log("You are eligible to vote!");
} else {
console.log("You are not eligible yet.");
}
✅ Output: You are not eligible yet.
3️⃣ if...else if...else Ladder
When you have multiple conditions, use else if to check each one step by step.
let marks = 85;
if (marks >= 90) {
console.log("Grade A+");
} else if (marks >= 75) {
console.log("Grade A");
} else if (marks >= 50) {
console.log("Grade B");
} else {
console.log("Fail");
}
✅ Output: Grade A
4️⃣ The switch Statement
The switch statement is a cleaner alternative to multiple if-else conditions. It checks a value against multiple cases.
let day = "Tuesday";
switch(day) {
case "Monday":
console.log("Start of the week!");
break;
case "Tuesday":
console.log("Keep going strong!");
break;
case "Friday":
console.log("Weekend is near!");
break;
default:
console.log("Enjoy your day!");
}
✅ Output: Keep going strong!
Best Practices for Conditional Statements
- Use === instead of == to avoid type conversion issues.
- Keep conditions simple and readable.
- Use switch when you have many options for a single variable.
- Always include a
default case in your switch statements.
Common Real-World Examples
// Example 1: Login check
let isLoggedIn = false;
if (!isLoggedIn) {
console.log("Please log in to continue.");
}
// Example 2: Weather condition
let temperature = 32;
if (temperature > 30) {
console.log("It's a hot day!");
} else {
console.log("The weather is pleasant.");
}
💡 Quick Summary
- if → checks a single condition
- else if → checks multiple conditions
- switch → handles many specific cases
🎯 SEO Tip:
If you’re searching for topics like “how to use if-else in JavaScript” or “JavaScript switch statement examples”, this guide covers all you need — from basic to advanced levels.
📚 FAQ (Frequently Asked Questions)
Q1: What is the difference between if and switch in JavaScript?
if is used for evaluating multiple unrelated conditions, while switch is best when comparing one variable to several possible values.
Q2: Can I use if inside a switch statement?
Yes, you can use an if statement inside a switch block for additional checks.
Q3: Which is faster — if or switch?
Performance difference is minimal, but switch is generally faster when checking multiple fixed values.
Next Lesson:
In the next part, we’ll cover Loops in JavaScript (for, while, do-while) — so you can repeat tasks efficiently.
Keep learning, keep coding 🚀