We have two types of conditional statements in JavaScript one is if else statements and other is switch case break statements.
JavaScript If statements
In JavaScript we use if statements to check if one condition we are applying if if block is true once that get’s true the block inside if condition runs. Check in following message i am setting up a variable var name = ateeq; and below i will check in if statement if my name is ateeq then say you are ateeq.
To compare values in conditional statement we have following operators.
Double Equal ==
myName == ‘ateeq’ Doube equal checks if both side are equal on other side you know single equal makes left side equal to right side but double equal check if both sides have same values and they are equal.
Triple Equal ===
Just like double Equal triple equal also checks if both sides are equal but in addition this checks if both sides have same data format like if both sides are number, if both sides are string, like this.
Not Equal !=
Not Equal sign returns true if ateeq != myName means if left side is not equal to right side.
Greater than or equal >=
This operator shows if both sides are not equal but if left side is greater than right side in that case this will return true.
There are some more operators but these are important operators.
[code lang=”js”]
<script type="text/javascript">
var myName = "Ateeq"; //declaring variable of MyName
//if condition. Double Equal Example
if(myName == "Ateeq") {
alert("You are Ateeq!");
}
//if condition. Not Equal Example
if(myName != "ATeeq") {
alert("You are not Ateeq At all :)");
}
//if condition. Greater than.
if(8 >= 4) {
alert("8 Is greater than 4");
}
//if Condition. Less Than Example.
if(4 < 5) {
alert("5 is less than 4");
}
</script>
[/code]
But what if you have to check multiple statements in one conditional statement? In that case you need OR || or AND && operators.
Suppose you have two statements to check if your name is equal to ateeq and you are a male, If we use OR condition then one of these both conditions have to become ture to run the code block inside conditional block. And if you use AND operator then both conditions have to become true to run the code block.
Example
[code lang=”js”]
<script type="text/javascript">
var myName = "Ateeq"; //declaring variable of MyName
var myFriend = "usman";
var myGender = "male";
//if condition checking my and my friend’s name
if(myName == "Ateeq" && myFriend == "usman"){
alert("You are Ateeq and Usman is your Friend.");
}
//if condition checking if i am ateeq or you are female.
if(myName == "Ateeq" || myGender == "female") {
alert("you are Ateeq or you are Female!");
}
</script>
[/code]
If else Statement.
In JavaScript we use else statement with if to run other statement in case of condition returns false. Like if you are checking your name and you write in if condition wrong name then if condition will not run but else condition will run the code. You can say if my name is ateeq then show you are ateeq else show you are not ateeq. This is very useful conditional statement in JavaSCript.
Example If else conditional statement JavaScript
[code lang=”js”]
<script type="text/javascript">
var myName = "Ateeq"; //declaring variable of MyName
var myFriend = "usman";
//if else condition checking name
if(myName == "Atique") {
alert("Your Name is Atique");
} else {
alert("You are Not Ateeq 🙂 ");
}
//if else condition checking my and my friend’s name
if(myName == "Ateeq" && myFriend == "usman"){
alert("You are Ateeq and Usman is your Friend.");
} else {
alert("You are not Ateeq Usman is not your Friend.");
}
</script>
[/code]
JavaScript conditional Switch Statements.
If you have many things to check and in case of one gets true you want to run something if else is very lengthy for that but switch statement is very easy and good idea to use cause that gets break after one condition case becomes true. But in if else all statements would run unless one gets true.
Suppose you are checking if today is Monday, Sunday, Tuseday or any day of week. And you want to call today is Monday or whatever day it is. The best thing to use is switch statement. and break it once condition becomes true.
Example of JavaScript Switch Statement.
[code lang=”js”]
<script type="text/javascript">
var day = "Tuseday"; //Declaring Day variable.
//switch statement to check days in different cases.
switch(day) {
case ‘Monday’: alert(‘Today is Monday.’);
break;
case ‘Tuseday’: alert(‘Today is Tuseday.’);
break;
case ‘Wednesday’: alert(‘Today is Wednesday.’);
break;
case ‘Thursday’: alert(‘Today is Thursday.’);
break;
case ‘Friday’: alert(‘Today is Friday.’);
break;
case ‘Saturday’: alert(‘Today is Saturday.’);
break;
case ‘Sunday’: alert(‘Today is Sunday.’);
break;
default: alert(‘Today is ummm! i dont know really :(.’);
break;
}
//switch block ends here.
</script>
[/code]
So basically switch statement take a expression to run cases on that and once a case gets true switch breaks and run the block for that case. This is the way how switch statement works in JavaScript.