Logical operators are mostly consist of AND OR NOT and its combinations. We will discuss more on this with examples for better understanding.
We will use if condition to check and then document.write to print the result. Here is the code.
AND is also written as &&
OR is written as ||
NOT is written as !
NOT EQAL TO !=
var my_var=5; var my_var2="plus2net";
Here is a table with different examples of logical operators
my_var=="5" && my_var2=="plus2net"
True
my_var==="5" && my_var2=="plus2net"
False
my_var==5 || my_var2=="plus2net"
True
my_var==4 || my_var2=="plus2net"
True
Equal to operator ( = )
We can check the data present at left and right side of the equal to operator and display the output. Here is an example .
<script type="text/javascript">
var my_var=5;
document.write(my_var=="5"); // Output is true
</script>
In the above example we have matched data stored in variable with 5 and found it to be True. Note that the data is a number and we are matching it with a string. So irrespective of type of variable it is matching. If we want to consider the type of variable while matching then we will use like this
var my_var=5;
document.write(my_var==="5"); // Output is false
We can make the variable as string and then check
var my_var="5";
document.write(my_var==="5"); // Output is true
Using Logical operators
In our scripts we will be using logical operators to match different conditions. Let us try some examples to understand its uses