
In this post, we will be learning about JavaScript's comparison operators.
Comparisons Operators
Comparison operators are used for determining whether a literal equal or differs from another literal.
Two equal signs ("==") will check only the value of literals, while three equal signs ("===") checks whether they are both the same value and the same type. For example, if x equals 6, then the operators below will return the value listed in the return column.
| Operator | Description | Comparing | Returns | ||
| "==" | equals to | x == 9 | false | ||
| x == 6 | true | ||||
| x == "6" | true | ||||
| "===" | equals to | x === 9 | false | ||
| x == 6 | true | ||||
| x == "6" | false |
Of particular note is that for two equal signs, 6 and "6"(a string) are equal, while for three equal signs they are not.
How Comparison Operators Can be Used
Comparisons are used to determine whether a variable matches a certain condition.
If the condition matches (the condition is true), we execute some block of code. Otherwise, (the condition is false), we execute another block of code.
JavaScript Code
if(age<18)
{
document.write("Sorry, you are too young");
}
else
{
document.write("Congratulation, you are old enough!");
}
Logical Operator
We use logical operators to specify relations between conditions.
For example, we can see whether a variable meets condition A and condition B or we can see whether a variable meets condition A or condition B.
| Operator | Description | Comparing | ||||||
| "==" | and | (x < 10 && y > 1) is true | ||||||
| " | " | or | (x == 5 | y == 5) is false | ||||
| "!" | not | (x != y) is true |
Comparing Different Types
| Case | Value |
| 2 < 12 | true |
| 2 < "12" | false |
| 2 < "John" | false |
| 2 > "John" | false |
| 2 == "John" | false |
| "2" < "12" | false |
| "2" > "12" | true |
| "2" == "12" | false |
The above are some examples of types being compared.
About the author
I'm a full-stack web developer who loves building new projects and learning new things
It might not show from my posts, but I'm also quite funny.
Comments
Join the discussionNo comments yet. Be the first to comment.




