In Js, Conditional Statements used to make decisions based on condition to lead right output.
They allow your program to choose different actions based on different conditions.
Without conditional statements, your program would run the same way every time
Just like a robot
with no thinking.
Basic check if a condition is true.
It is used for Check condition, if it is true then execute sample code.
If condition is not match. its simply ingnores.
if (age > 18) {
alert("You can vote");
}
Consider Above snippet, And lets give it an age. (Age = 10),
let age = 20;
if (age > 18) {
console.log("You can vote");
}
Output,
You can vote
Now lets give age below 18,
let age = 10;
if (age > 18) {
console.log("You can vote");
}
Its Does not return anything, simply Ingnores and continue to execute if condition not match.
Choose between two options.
This is Same as if Statement. but instead of ingnoring statement at not matching condition.
It Execute alternative code defined in else case.
if (marks >= 33) {
alert("Pass");
} else {
alert("Fail");
}
Let's consider marks above and below then condition and check. it will execute if case or else case.
marks = 40
let marks = 40;
if (marks >= 33) {
console.log("Pass");
} else {
console.log("Fail");
}
Pass
marks = 30
let marks = 30;
if (marks >= 33) {
console.log("Pass");
} else {
console.log("Fail");
}
Fail
Check multiple conditions in order.
This is Same as if else Statement. But this Allows to check multiple conditions.
if first is not match then go to next else if condition to check.
Let's consider marks again but in Different classification. it will execute and check which case need to execute.
if (marks >= 90) {
alert("A grade");
}
else if (marks >= 75) {
alert("B grade");
}
else if(marks >= 40) {
alert("C grade");
}
else{
alert("D grade");
}
Lets consider marks = 99
let marks = 99;
if (marks >= 90) {
console.log("A grade"); // A grade, 99 is greater than 90
}
else if (marks >= 75) {
console.log("B grade");
}
else if(marks >= 40) {
console.log("C grade");
}
else{
console.log("D grade");
}
A grade
Lets consider marks = 80
let marks = 80;
if (marks >= 90) {
console.log("A grade");
}
else if (marks >= 75) {
console.log("B grade"); // B grade, 80 is greater than 75
}
else if(marks >= 40) {
console.log("C grade");
}
else{
console.log("D grade");
}
B grade
Lets consider marks = 55
let marks = 50;
if (marks >= 90) {
console.log("A grade");
}
else if (marks >= 75) {
console.log("B grade");
}
else if(marks >= 40) {
console.log("C grade"); // C grade, 50 is greater than 40
}
else{
console.log("D grade");
}
C grade
Lets consider marks lower than 40
let marks = 30;
if (marks >= 90) {
console.log("A grade");
}
else if (marks >= 75) {
console.log("B grade");
}
else if(marks >= 40) {
console.log("C grade");
}
else{
console.log("D grade"); // D grade, 30 is lower than 40
}
D grade
If not any mensioned condition get true, it execute last else statement
Best when checking one variable with many values:
For example we have More than one value to check for it.
• by if else each value need to define different condition saperatly.
• if else is not so effective with multiple value checking at once.
• Also required to use operator to compare with.
• If else compare with each condition.
let day = 2;
if(day == 1){
console.log("Monday");
}
else if(day == 2){
console.log("Tuesday");
}
else if(day == 3){
console.log("Wednesday");
}
else if(day == 4){
console.log("Thursday");
}
else if(day == 5){
console.log("Friday");
}
else if(day == 6){
console.log("Saturday");
}
else if(day == 7){
console.log("Sunday");
}
else{
console.log("Invalid");
}
Here Switch Case handle the case.
• Best when checking one variable against fixed values.
• No complex validation using <, >, == or using any operator
• Clean and structured code.
• Only check to the existing Case.
let day = 2;
switch(day) {
case "1":
alert("Monday");
break;
case "2":
alert("Tuesday");
break;
case "3":
alert("Wednesday");
break;
case "4":
alert("Thursday");
break;
case "5":
alert("Friday");
break;
case "6":
alert("Saturday");
break;
case "7":
alert("Sunday");
break;
default:
alert("Invalid");
}
Tuesday