Conditional Statements are used to make decisions in JavaScript.
They allow a program to execute different blocks of code depending on whether a condition is true or false.
JavaScript provides the following conditional statements:
- if Statement
- if...else Statement
- if...else if...else Statement
- switch Statement
The if statement executes a block of code only when the specified condition is true.
if(condition){
// code to execute
}var age = 18;
if(age >= 18){
console.log("You are eligible to vote.");
}You are eligible to vote.
- The condition is:
age >= 18- If the condition is true, the code inside the if block is executed.
- Since age is 18, the message is displayed.
The else statement executes when the if condition is false.
if(condition){
// Executes if condition is true
}
else{
// Executes if condition is false
}var age = 15;
if(age >= 18){
console.log("Eligible to Vote");
}
else{
console.log("Not Eligible to Vote");
}Not Eligible to Vote
Since:
15 >= 18is false,
the else block is executed.
This statement is used when there are multiple conditions.
if(condition1){
// code
}
else if(condition2){
// code
}
else{
// code
}var age = 16;
if(age >= 18){
console.log("You are eligible to vote.");
}
else if(age >= 16){
console.log("You are eligible for a learner's permit.");
}
else{
console.log("You are not eligible.");
}You are eligible for a learner's permit.
First Condition:
age >= 18Result:
false
Second Condition:
age >= 16Result:
true
Hence the second block executes.
var marks = 82;
if(marks >= 90){
console.log("Grade A");
}
else if(marks >= 75){
console.log("Grade B");
}
else if(marks >= 50){
console.log("Grade C");
}
else{
console.log("Fail");
}Grade B
The switch statement is used as an alternative to multiple if-else statements.
It checks the value of an expression and executes the matching case.
switch(expression){
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}var day = "Monday";
switch(day){
case "Monday":
console.log("Today is Monday.");
break;
case "Tuesday":
console.log("Today is Tuesday.");
break;
case "Wednesday":
console.log("Today is Wednesday.");
break;
default:
console.log("It's not Monday, Tuesday, or Wednesday.");
}Today is Monday.
The switch statement compares:
daywith:
"Monday"Since both are equal,
this block executes:
console.log("Today is Monday.");The break statement stops further execution.
Consider:
var day = "Monday";
switch(day){
case "Monday":
console.log("Monday");
case "Tuesday":
console.log("Tuesday");
case "Wednesday":
console.log("Wednesday");
}Monday
Tuesday
Wednesday
Because there is no break statement, all remaining cases execute.
var day = "Monday";
switch(day){
case "Monday":
console.log("Monday");
break;
case "Tuesday":
console.log("Tuesday");
break;
}Monday
An if statement inside another if statement is called Nested if.
var age = 20;
var citizen = true;
if(age >= 18){
if(citizen == true){
console.log("Eligible to Vote");
}
}Eligible to Vote
| if-else | switch |
|---|---|
| Used for conditions | Used for fixed values |
| Supports relational operators | Uses exact matching |
| Suitable for ranges | Suitable for menu options |
| Slower for many conditions | Faster and cleaner |
- Conditional statements control the flow of execution.
ifexecutes only when the condition is true.elseexecutes when the condition is false.else ifis used for multiple conditions.switchis an alternative to multiple if-else statements.breakstops further execution in switch.defaultexecutes when no case matches.
Conditional Statements are one of the most important concepts in JavaScript. They help programmers make decisions, validate conditions, execute different code blocks, and control program flow. The if, else, else if, and switch statements are widely used in web development for form validation, authentication systems, menu-driven programs, and business logic implementations.