JavaScript Comparison operators are mostly used either in If Statements or Loops. Comparison operators 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. Below table shows all the Relational Operators in JavaScript Programming with examples.
JavaScript Comparison Operators | Usage | Description | Example |
---|---|---|---|
> | a > b | a is greater than b | 7 > 3 returns True |
< | a < b | a is less than b | 7 < 3 returns False |
>= | a >= b | a is greater than or equal to b | 7 >= 3 returns True |
<= | a <= b | a is less than or equal to b | 7 <= 3 return False |
== | a == b | a is equal to b | 7 == “7” returns True |
=== | a === 7 | a is 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 Comparison Operators practically. For this example, We are using two variables a and b and their values are 9 and 4. We are going to use these two variables to perform various relational operations present in JavaScript Programming Language
JAVASCRIPT CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!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> |
OUTPUT
ANALYSIS
We assigned 2 integer values a, b and we assigned the values 9 and 4 using the below statement.
1 |
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 are used in If Else Statement. For this 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.
JAVASCRIPT CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!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> |
ANALYSIS
We assigned 2 integer values x, y and we assigned the values 10 and 25 using the below statement.
1 |
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
1 |
document.getElementById("result").innerHTML = x +' is Equal to ' + y; |
1 |
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 example helps you to understand, Difference between == operator, === operator and != operator, !== operator.
- == check whether the value on left hand side is equal to value on right hand side
- === check whether the value and the data type on left hand side is equal to value and the data type on right hand side
- != check whether the value on left hand side is not equal to value on right hand side
- !== check whether the value and the data type on left hand side is not equal to value and the data type on right hand side
JAVASCRIPT CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!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> |
OUTPUT
Thank You for Visiting Our Blog.
Leave a Reply