5 ms·
In CoffeeScript I discovered the other day that: 0 <= "" <= 0 # True "" is 0 # False
by mck- 13y ago
In CoffeeScript I discovered the other day that:
0 <= "" <= 0 # True
"" is 0 # False
- shoo 13y agoI guess that is because coffeescript's <= operator is just javascript's <= operator, which does implicit type conversion. but coffeescript's "is", aka ==, aka === in javascript, doesn't. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... having "==" be semantically quite different to "<=" doesn't seem a particularly nice choice of notation. arguably coffeescript has made things worse in this case compared to plain javascript, where you're probably not going to expect === to behave similarly to <=. not obvious how you'd improve this to make it consistent, without say redefining how "==" and "<=" work to make them raise type errors or evaluate to something undefined if the types of the arguments don't match.
- runlevel1 13y agoIt's because equality comparisons in CoffeeScript (==, !=, is, isnt) are always compiled into strict (non-type-converting) comparisons (===, !==). [1] JavaScript doesn't have strict relational operators (>, <, >=, <=), so all relational comparisons undergo type-conversion. [2] CoffeeScript: 0 <= "" <= 0 # True JavaScript: (0 <= "" && "" <= 0); // True CoffeeScript: "" is 0 # False JavaScript: "" === 0; // False [1]: http://coffeescript.org/#operators http://coffeescript.org/#operators [2]: http://www.ecma-international.org/ecma-262/5.1/#sec-11.8 http://www.ecma-international.org/ecma-262/5.1/#sec-11.8 EDIT: Forgot to include 'is' and 'isnt' operators.