Relational Operators
This
section
describes the JavaScript relational operators. These are operators
that test for a relationship (such as “less-than” or
“property-of”) between two values and return
true or false depending on
whether that relationship exists. As we’ll see in Chapter 6, they are most commonly used in things like
if statements and while loops,
to control the flow of program execution.
Comparison Operators
The most commonly used types of relational operators are the comparison operators, which are used to determine the relative order of two values. The comparison operators are:
-
Less than (
<) The
<operator evaluates totrueif its first operand is less than its second operand; otherwise it evaluates tofalse.-
Greater than (
>) The
>operator evaluates totrueif its first operand is greater than its second operand; otherwise it evaluates tofalse.-
Less than or equal (
<=) The
<=operator evaluates totrueif its first operand is less than or equal to its second operand; otherwise it evaluates tofalse.-
Greater than or equal (
>=) The
>=operator evaluates totrueif its first operand is greater than or equal to its second operand; otherwise it evaluates tofalse.
The operands of these comparison operators may be of any type. Comparison can be performed only on numbers and strings, however, so operands that are not numbers or strings are converted. Comparison and conversion occur as follows:
If both operands are numbers, or if both convert to numbers, they ...