JavaScript » Comparison

A comparison operator compares two operands and returns a Boolean value (true or false) as to the validity of the comparison. Operands can be of numeric or string type.

== Operator


This is the equal operator and returns a boolean true if both the operands are equal. JavaScript will attempt to convert different data types to the same type in order to make the comparison.

Examples

Code:
a == 2
a == "2"
2 == '2
Explanation:

Assuming 'a' to be 2 and 'b' to be 4, the above examples will return a value of true.


!= Operator


This is the not equal operator which returns a Boolean true if both the operands are not equal. Javascript attempts to convert different data types to the same type before making the comparison.

Examples

Code:
a != b
a != 4
a != "2"
Explanation:

The above examples return a Boolean true.

=== Operator

This is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type.

Code:
a === 2
b === 4
Explanation:

These above examples return true.

!== Operator


This is the strict not equal operator and only returns a value of true if both the operands are not equal and/or not of the same type.

Examples

Code:
a !== b
a !== "2"
4 !== '4'
Explanation:

The above examples return a Boolean true.

> Operator


This is the greater than operator and returns a value of true if the left operand is greater than the right.

Examples

Code:
a > 1
b > a

>= Operator


This is the greater than or equal operator, which returns true if the first operand is greater than or equal to the second.

Examples

Code:
a >= 1
a >= 2
b >= a
Explanation:

The above examples return true.

< Operator


This is the less than operator and returns true if the left operand is less than the right.

Examples

Code:
a < 3
a < b

<= Operator


This is the less than or equal operator and returns true if the first operand is less than or equal to the second.

Examples

Code:
a <= 2
a <= 3
a <= b
Explanation:

The above examples all return true.