Saturday, September 27, 2008

The == Operator in JavaScript

While working on the type constraint system, we got bitten by a pretty unexpected behavior of the == operator in JavaScript, that I wasn't aware of before. In our case is made a test case related to coercion pass, although no coercion was actually applied.
Here are some examples from the firebug console, that show the behavior:
>>> 1 == true
true
>>> 0 == false
true
So 1 equals true and 0 equals false. This can actually come in quite handy in many situations, where you are working with languages that have a similar understanding of truthness on the server side. Using the === obviously helps in this situation.
Post a Comment