AND OR NOT Equal logical operators

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

<script type="text/javascript">

var my_var = 5;

if(my_var=="5"){document.write("True")}
else{document.write("False")}
// output True



if(my_var< 6){document.write("True")}
else{document.write("False")}
// output True


if(my_var<=5){document.write("True")}
else{document.write("False")}
// output True

if(my_var !=5){document.write("True")}
else{document.write("False")}
// output False


if(my_var>=5){document.write("True")}
else{document.write("False")}
// output True




if(my_var>=5){document.write("True")}
else{document.write("False")}
// output True



if(!(my_var > 4)){document.write("True")}
else{document.write("False")}
// output True




if(my_var==="5"){document.write("True")}
else{document.write("False")}
// output False



if(my_var=="4"){document.write("True")}
else{document.write("False")}
// output False



if(my_var==5){document.write("True")}
else{document.write("False")}
// output True



if(my_var!=4){document.write("True")}
else{document.write("False")}
// output True



if(my_var !=5){document.write("True")}
else{document.write("False")}
// output False



if(my_var !=="5"){document.write("True")}
else{document.write("False")}
// output True


var my_var2="plus2net";


if(my_var=="5" && my_var2=="plus2net"){document.write("True")}
else{document.write("False")}
// output True



if(my_var==="5" && my_var2=="plus2net"){document.write("True")}
else{document.write("False")}
// output False

if(my_var==5 || my_var2=="plus2net"){document.write("True")}
else{document.write("False")}
// output True



if(my_var==4 || my_var2=="plus2net"){document.write("True")}
else{document.write("False")}
// output True

</script>

More on comparison operators

Be the first to post comment on this article :


Google+

Post Comment This is for short comments only. Use the forum for more discussions.






OPINION POLL

How you handle Search Engine Optimization ( SEO ) for your website
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2015 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer