Comparison Operators:
They are used to compare numerical (numbers), string or boolean values.
| Operator-Syntax | Description |
| == | validates the condition whether two numbers or string values are equal |
| != | validates the condition whether two numbers or string values are not equal |
| > | validates the condition whether one numbers is greater than other |
| < | validates the condition whether one numbers is less than other |
| >= | compares the numbers and checks whether one numbers is greater than or equal to other |
| <= | compares the numbers and checks whether one numbers is less than or equals other |
| === | used to compare and check whether two values are strictly equal |
| !== | used to compare and check whether two values are strictly not equal |
The main difference between "equal to (==)" and "strictly equal to (===)" is that
the equality comparison takes place after type conversion for "equal to (==)" and before
type conversion for "strictly equal to (===)".
i.e "5" == 5 and "5" !== 5
Example Code:
<script language="javascript">
var a = "5";
var b = 5;
if(a == b)
{
document.write(" Testing Comparative Operator for equals ");
}
if(a === b)
{
document.write(" Testing Comparison Operator for strictly equals ");
}
</script>
Result:
As a is not strictly equal to b the second message is not printed.
|