JavaScript Comparison operators are mostly used either in If Statements or Loops. Comparison operators in JavaScript are commonly used to check the relationship between two variables. If the relation is true, then it will return Boolean TRUE, or if the relation is false, then it will return Boolean FALSE.

The below table shows all the Relational Operators in JavaScript Programming with examples.

JavaScript Comparison Operators | Usage | Description | Example |
---|---|---|---|

> | a > b | a greater than b | 7 > 3 returns True |

< | a < b | a less than b | 7 < 3 returns False |

>= | a >= b | a greater than or equal to b | 7 >= 3 returns True |

<= | a <= b | a less than or equal to b | 7 <= 3 return False |

== | a == b | a equal to b | 7 == “7” returns True |

=== | a === 7 | a Exactly equal to b | 7 === “7” returns False |

!= | a != b | a is not equal to b | 7 != 3 returns True |

!== | a !== b | a is not equal to b and its Type | 7 !== 3 returns True |

## JavaScript Comparison Operators Example

This example helps you to understand the JavaScript Comparison Operators practically. For this example, We are using two variables a and b and their values are 9 and 4. Next, we are going to use these two variables to perform various relational operations in JavaScript Programming Language

<!DOCTYPE html> <html> <head> <title>JavaScript Comparison Operators </title> </head> <body> <h1>Performing Comparison Operations </h1> <script> var a = 12, b = 4; document.write("Result of " + a +" Greater than " + b +" is = " + (a > b) + "<br />"); document.write("Result of " + a +' Greater than or Equal to ' + b +" is = " + (a >= b) + "<br />"); document.write("Result of " + a +' Less than or Equal to ' + b +" is = " + (a <= b) + "<br />"); document.write("Result of " + a +' Less than ' + b +" is = " + (a < b) + "<br />"); document.write("Result of " + a +' Equal to ' + b +" is = " + (a ==b ) + "<br />"); document.write("Result of " + a +' Not Equal to ' + b +" is = " + (a !=b ) + "<br />"); </script> </body> </html>

In this JavaScript Comparison Operators example, we assigned 2 integer values a, b and we assigned the values 9 and 4 using the below statement.

var a = 12, b = 4;

In the next line, We checked these values against each and every Comparison operator we have.

## JavaScript Comparison Operators using If Condition

This example helps you to understand how JavaScript Comparison operators used in If Else Statement. For this JavaScript example, We are using two variables x and y, and their values are 10 and 25. We are going to use == operator to check whether variables x is equal to y or not.

<!DOCTYPE html> <html> <head> <title>Javascript Comparison Operators 2</title> </head> <body> <h1> Comparison Operations </h1> <p id = 'result'> Addition </p> <script> var x = 12, y = 24; if (x === y) { document.getElementById("result").innerHTML = x +' is Equal to ' + y; } else { document.getElementById("result").innerHTML = x +' is Not Equal to ' + y; } </script> </body> </html>

```
Comparison Operations
12 is Not Equal to 24
```

In this JavaScript relational Operators example, We assigned 2 integer values x, y and we assigned the values 10 and 25 using the below statement.

var x = 12, y = 24;

If Condition

If x is exactly equal to y then first statement (statement inside the If block) will be executed

document.getElementById("result").innerHTML = x +' is Equal to ' + y;

If x is not equal to y, then the second statement (statement inside the Else block) will be executed.

document.getElementById("result").innerHTML = x +' is Not Equal to ' + y;

Here x value (12) is obviously not equal to 25 so, Second statement will be displayed in the paragraph

## Difference between == , === and !=, !==

This JavaScript Comparison Operators example helps you to understand, Difference between == operator, === operator and != operator, !== operator.

- == check whether the value on the left-hand side is equal to the value on the right-hand side
- === check whether the value and the data type on the left-hand side is equal to the value and the data type on the right-hand side
- In JavaScript Comparison Operators, != check whether the value on the left-hand side is not equal to the value on the right-hand side
- !== check whether the value and the data type on the left-hand side is not equal to the value and the data type on the right-hand side

<!DOCTYPE html> <html> <head> <title>Javascript Comparison Operators </title> </head> <body> <h1> Comparison Operations </h1> <script> document.write("Equal to: " + (12 == "12")); document.write(" <br /> Exactly Equal to: " + (12 === "12")); document.write(" <br /> Not Equal to: " + (12 != "12")); document.write(" <br /> Exactly Not Equal to: " + (12 !== "12")); </script> </body> </html>

```
Comparison Operations
Equal to: true
Exactly Equal to: false
Not Equal to: false
Exactly Not Equal to: true
```