Skip to main content

JavaScript Comparison Operators

Published 2 min readMarkdown
JavaScript Comparison Operators

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.

OperatorDescriptionComparingReturns
"=="equals tox == 9false
x == 6true
x == "6"true
"==="equals tox === 9false
x == 6true
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.

OperatorDescriptionComparing
"=="and(x < 10 && y > 1) is true
""or(x == 5y == 5) is false
"!"not(x != y) is true

Comparing Different Types

CaseValue
2 < 12true
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.

No comments yet. Be the first to comment.

More from this blog