4 ms·
Javascript's maze of type coercion rules produce results which manage to be frequently wrong but in ever more bewildering ways only slightly less crackheaded th
by prodigal_erik 14y ago
Javascript's maze of type coercion rules produce results which manage to be frequently wrong but in ever more bewildering ways only slightly less crackheaded than PHP. It's also saddled with Java's inexcusable "everything is an object, except for all the crap that isn't" philosophy. http://wtfjs.com/ http://wtfjs.com/ is a growing list, but a few of my favorites:
[] != []
[] == ![]
{} + [] is 0
[] + {} is "[object Object]"
{} + {} is NaN
- lifthrasiir 14y agoTo be exact, {} + {} is NaN only when used as a statement. In fact it is equivalent to {}; +{} which results in the last statement being evaluated as NaN. When you put parentheses around it (e.g. ({}+{})) it evaluates to "[object Object][object Object]", which should be obvious from [] + {} if it were indeed obvious.
- saurik 14y agoYeah, the people talking about this are talking about it due to a five-minute lightning talk, and honestly it really bothers me that he is making people think that adding two objects together has that behavior when in fact it has nothing to do with objects at all: it has much more to do with automatic semi-colon insertion than with scary type conversion.