A comparison operator compares its operands based on standard lexicographical ordering, using Unicode values. It returns a logical value based on whether the comparison is true. The operands can be numerical or string values.
Operator |
Name |
Description. Returns true if: |
True if a = 2, b = 5: |
== |
Equal |
The operands are equal. If they are not of the same type, JavaScript tries to convert them to an appropriate type for the comparison. |
2 == a 2 == '2' "2" == a |
!= |
Not equal |
The operands are not equal. If they are not of the same type, JavaScript tries to convert them to an appropriate type for the comparison. |
a != 5 b != "2" |
=== |
Strict equal |
The operands are equal and of the same type. |
2 === a |
!== |
Strict not equal |
The operands are not equal and/or not of the same type. |
a !== "2" 2 !== '2' |
> |
Greater than |
The left operand is greater than the right operand. |
b > a |
>= |
Greater than or equal |
The left operand is greater than or equal to the right operand. |
b >= a a >= 2 |
< |
Less than |
The left operand is less than the right operand. |
a < b |
<= |
Less than or equal |
The left operand is less than or equal to the right operand. |
a <= b b <= 5 |
For example:
var a=2;
var b=5;
if (a == b)
{
document.writeln("Behold! a and b are the same!");
}
The coode fragment above would cause no alert, whereas the code fragment below would
var c=5;
var d=5;
if (c == d)
{
document.writeln("Behold! c and d are the same!");
}
Click here to see more about if and else statements.
Back to top Home Serious Stuff
© 2002. Copyright Sue Nethercott.