Comparison Operators in JavaScript

Some time while performing comparison between different data types we got unexpected results. This mostly occurs when we compare a string with number. Some first the JavaScript should convert to a string to accurate results.

JavaScript Comparison Operators

Operator Description
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator

Example of Less than Operator

In this example < less than operator is used for comparison. This will return true or false.

<script type = "text/javascript">
var a = 3, b = 2;

document.write(a < 2); 
document.write("<br/>");
document.write(b < 3); 
</script>                  
        

Output of above code


Example of Less than or Equal to Operator

In this example <= less than or equal operator is used for comparison. It will return true or false.

<script type = "text/javascript">
var a = 2;

document.write(a <= 3) 
document.write("<br/>")
document.write(a <= 2);
</script>                  
        

Output of above code


Example of Greater than Operator

In this example > greater than operator is used for comaparison. It will return true or false.

<script type = "text/javascript">
var a = 3;

document.write(a > 2);
</script>                  
        

Output of above code


Example of Greater than or Equal to Operator

In this example >= greater than or equal to operator is used for comparison. If comparison will be true than answer will be true else it will be false.

<script type = "text/javascript">
var a = 3;

document.write(a >= 3); 
</script>                  
        

Output of above code








The Best

Comment here

If you have any query, if you want to know something about any of technical course related to computer science field, if you have any suggestion about relevant to uploaded content or if you anything wrong here (any mistake in content) than please contact us. Keep in mind, comment should be according to community guidelines.