0

So i've been looking into type coercion and i get the basics, like how:

true + 0 = 1;
false + 0 = 0;

while messing around in the console I noticed that

({}) + true = "[Object Object]true";
{} + true = 1;

my question being why does {} get changed to [Object Object] when in () and not when just added to true without the ()?

  • 1
    Short version `({})` is an object, while your `{}` isn't. – VLAZ Sep 12 '16 at 21:17
  • 2
    ^ Specifically, `{}` is viewed as a block (like what you'd place your `for` loop code in) so really you're executing an empty block followed by `+true`. – Mike Cluck Sep 12 '16 at 21:19
  • ```{}``` in expression is a block hence like @mike-c said it's just empty block +true. In a statement like ```var a = {} + true``` the ```{}``` is an object and hence ```a``` will contain ```[Object Object]true``` – qballer Sep 12 '16 at 21:27

0 Answers0