Just tried this in my browser's console:
!!0 //=> false
{} + [] //=> 0
!!({} + []) //=> true
What's going on here!?
Edit: I'm talking about {} + []
(object first) which is different from [] + {}
(array first), the latter always returns 0
. Spacing ({}+[]
vs {} + []
) doesn't seem to matter
Doing ({} + [])
produces the string [object Object]
which when coerced into a boolean is a true value.
Why it treats it as a string concatenation when wrapped in parentheses and a number when not wrapped, I'm trying to figure out.
Update: I think I've figured it out. This trick happens when the code at a global level, i.e. not as an assignment or as an argument. What's probably happening is it's interpreting the {}
as a block rather than an object.
If you type { var x = 4; } +[]
into your console you'll get the same result as {}+[]
. Where as if you place it inside of a set of parentheses it forces the compiler to interpret the interior as an expression i.e. an Object
plus an Array
which produces the string.
My guess is this change is due in part to the change in scope handling due to let
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With