HTML and CSS Reference
In-Depth Information
Comparing Numbers.
When the comparison operators are used to compare num-
bers, numeric values are compared; as in, is
50 > 45
? A Boolean value of either
true
or
false
is returned.
Table 5.9
Comparison Operators
Example
How Operator Compares Numbers
x
>
y
x
is greater than
y
x
>=
y
x
is greater than or equal to
y
x
<
y
x
is less than
y
x
<=
y
x
is less than or equal to
y
EXAMPLE
5.6
<html>
<head>
<title>Comparing Numbers</title>
</head>
<body>
<h3>
1
<script type = "text/javascript">
2
var x = 5;
var y = 4;
3
var result = x > y;
4
document.writeln("The result is "+
result
+ ".<br />");
5
result = x < y;
6
document.writeln( "The result is " +
result
+ ".<br />");
7
</script>
</h3>
</body>
</html>
EXPLANATION
1
The JavaScript program starts here.
2
The variables x and
y
are assigned values to be compared later in the program.
3
If the value of
x
is greater than the value of
y
, a Boolean value of either
true
or
false
is returned and assigned to the variable
result
.
4
The Boolean result of the comparison is displayed by the browser. It is
true
;
x
is
greater than
y
.