It’s easy to code through life without giving much thought to operator precedence. Of course we wax parenthetical from time to time, remembering my dear aunt sally. But for the most part, we can typically rely on the compiler to keep us safe from all sorts of logical missteps. For example, consider the following snippet:
“rose” == “rose” == “rose”
In C#, this simply doesn’t compute. The compiler asserts that it cannot apply the == operator to operands of type bool and string Fair enough. The first equality operation produces a bool and that bool is compared to a string. But Python’s (and therefore IronPython’s) egalitarian nature allows it to produce a True value from this statement. Each operand is as important as the other. After all a rose, is a rose, is a rose…
JavaScript sits somewhere in the middle. And that’s why I’m writing this post… I had a logical error in a script I had written and it was caused by my failure to recognize JavaScript’s operator precedence. JavaScript acts like C# in that it first evaluates a bool for the two most left operands. But it then allows the comparison to the string (it is not a statically typed language after all). So the above code returns false in JavaScript.
Minor details often cause the most headaches. This is one such example. As we increasingly become language switch-hitters, it’s that much more important to pay attention to the details.