In our JavaScript code we will use various comparison and logical operators to identify various conditions and accordingly execute different codes. It can be a simple comparison or a complex combination of logical operators and comparisons. These comparisons are mainly checking for equal to or NOT equal to status and using AND OR NOT logical operators within our JavaScript code.
We can understand it better by using some examples. To check the status we will be using document.write to print the result by saying 'True' or 'False' . We will be using one if condition for this.
Comparison operator
var my_var = 5;
if(my_var=="5"){document.write("True")}
else{document.write("False")}
// output False
You can see the above out put will be False
This way we can check different comparison conditions. Note that when we use == we are matching only value not type but when we use === we are comparing both type and value. Here is a short table to explain various conditions and its outputs for different if conditions. Note that we have changed the if condition checking only and in all the cases we have considered the value of my_var=5
var my_var=5;
my_var==5
True
my_var=="5"
True
my_var==="5"
False
my_var=="4"
False
my_var==4
False
You can see my_var==="5" has given us output False, because we are checking value and type both. But in case of my_var=="5" it is True as we y compare value only not type.
Less Than < , Greater Than >
We will continue with our variable like this my_var=5 , here is the table.