If both operands are strings, then they are compa red alphabetically.
If at least one operand is n ot a string, then both operands are converted to num-
bers and then compared.
If either operand is (or converts to) NaN, then a comparison always re turns
false.
In view of the second rule, the following two expression s will return true:
false < "1" //Returns true
2 < "14" //Returns true
The third rule tells us that the fo llowing two expression s will r eturn false:
"I’m the most epic line of computer code ever." > 0 //Returns false
"I’m the most epic line of computer code ever." < 0 //Returns false
Recall our last meeting, when we encou ntered th e following expression where x was
set to four:
10 < x < 20
Now you might be able to explain what the return value of this expression is a nd why.
Maria: You said the other day it wasn’t false so it must be true. OK, I guess
that x is first compared to 10 because of the left-to-right associativity of the less-than
operator. Because x is 4, this obviously r eturns false, which is then compared to 20.
The second of the above rules says that if at least one operand is not a string, it mu st
convert to a number. The false value is therefore converted to zero. Sinc e zero is
less than 20, the whole expression retur ns true.
Professor: Splendid! As a matter of fact, such an expression is meaningless for it
always returns true regardless of the type and value of x. If you want to che ck
whether x lies in an interval between 10 and 2 0, you sh ould write:
10 < x && x < 20
In that sense, you can look at the logical AND operator as the inte rsection operator.
The above expression returns true for all values of x that belong to the intersection
of two sets: the set of values greater than 10, and the set of values smaller than 20 .
Mike: Is it also true that logical OR can be seen as the union ope rator in situations
like this?
Professor: Yes. It is indeed.
7.4. Type Conversions 145
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.84.175