Conditional statements are used to perform different actions based on different conditions. They allow your code to make decisions and execute specific blocks of code only when certain criteria are met.
In JavaScript, we have the following conditional statements:
The if statement is the most basic control flow statement. It evaluates a condition inside
parentheses (). If the condition is truthy, the code block inside {} runs.
if (condition) {
// block of code to be executed if the condition is true
}
let hour = 10;
if (hour < 12) {
console.log("Good Morning!");
}
The else statement allows you to execute a block of code when the condition is false.
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
let age = 16;
if (age >= 18) {
console.log("You are eligible to vote.");
} else {
console.log("You are not eligible to vote.");
}
Use else if to specify a new condition if the first condition is false. You can chain multiple
else if statements.
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if the condition1 is false and condition2 is true
} else {
// code to be executed if the condition1 is false and condition2 is false
}
let time = 15; // 3 PM
if (time < 12) {
console.log("Good Morning");
} else if (time < 18) {
console.log("Good Afternoon");
} else {
console.log("Good Evening");
}
You can have if statements inside other if statements. This is useful for checking
multiple dependent conditions.
let num = 20;
if (num > 10) {
console.log("Greater than 10");
if (num > 30) {
console.log("Greater than 30");
} else {
console.log("But not greater than 30");
}
}
The switch statement is used to perform different actions based on different conditions. It is
often a cleaner alternative to a long if..else if ladder when comparing a single variable
against multiple values.
case.break keyword stops the execution inside the switch block.default keyword specifies the code to run if there is no match.
let day = 3;
let dayName;
switch(day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
}
console.log("Day is: " + dayName);
| Feature | if-else | switch |
|---|---|---|
| Usage | Checking ranges or boolean conditions | Checking discrete values (equality) |
| Expression | Evaluates to true/false | Evaluates to a value |
| Performance | Can be slower for many conditions | Generally faster for many fixed values |
| Flexibility | High (can use logical operators) | Low (only equality checks) |
switch when you are comparing a single variable against many known
values. Use if-else for ranges (e.g., age > 18) or complex logic.
Test your understanding of JavaScript Conditional Statements.
age and set it to 16.if...else statement to check if the age is 18 or greater.isRaining and set it to true.if...else if...else chain to grade a student's score.score of 85 in a variable.if...else block into a switch statement.Convert the following logic into a switch statement:
let color = "red";
if (color === "red") {
document.write("Stop");
} else if (color === "green") {
document.write("Go");
} else {
document.write("Unknown");
}
username = "admin" and password = "12345".if to check if the password is "secret".