Java Reference
In-Depth Information
16 != "16"; // type coercion makes these equal
<< false
16 !== "16";
<< true
As with equality, it is much better to use the hard inequality operator as this will give more
reliable results unaffected by type coercion.
Greater Than and Less Than
We can check if a value is greater than another using the > operator:
8 > 4; << true
You can also use the "less than" < operator in a similar way:
8 < 4; << false
If you want to check if a value is greater than or equal to another value, you can use the >=
operator, but be careful, the equality test works in the same way as the soft equality operat-
or:
8 >= 4;
<< true
8 >= 8;
<< true
8 >= "8";
<< true
As you can see, type coercion means that strings can be confused with numbers. Unfortu-
nately, there are no "hard" greater-than or equal-to operators, so an alternative way to avoid
type coercion is to use a combination of the greater-than operator, logical OR, and a hard
equality:
Search WWH ::




Custom Search