In the NodeJs console, using a non-strict comparison of an empty object to true or false I always get false. Why?
> ({}) == true || ({}) == false
false
Because you're comparing an object to a boolean. That's where things get complicated as you're not using a type-safe comparison.
Booleans get compared to other types as if they were numbers, i.e. true is casted to 1 first and false to 0. Then, when an object is compared to a number, it will be casted to a primitive value (without a preferred type) - invoking the DefaultValue algorithm. On plain objects, this will stringify them, and your empty object {} becomes "[object Object]", which is neither equal to 0 nor 1.
There are some objects however that will compare as equal to booleans, for example:
[0] == false
[1] == true
({toString:function(){return "1"}}) == true
({valueOf:function(){return 0}}) == false
({valueOf:function(){return true}}) == true
[object Object] will be cast to Number as well (when compared with booleans), becoming NaN in process. And NaN is obviously incomparable to anything. ) A short rule of thumb: when in doubt, add Strings, compare Numbers. Still, it's better never use == when comparing objects and primitives.DefaultValue operation (i.e. the object's toString/valueOf methods) yields. If that is a string or boolean, it will be parsed to a number to be comparable with 0 or 1.