Closed EmNudge closed 3 years ago
+[] // -> 0
0===+[] // -> true
{}+[] // -> 0
0==={}+[] // -> false 😕WTF??
a={}+[] // -> '[object Object]' 😕WTF??
(()=>{return {}+[]})() // -> '[object Object]'
(()=>({}+[]))() // -> '[object Object]'
(╯‵□′)╯︵┻━┻
Yes, because {}+[] is interpreted as
{
// a code block here
}
+[] // -> 0
although 0==={}+[]
is interpreted as 0 === ({} + [])
. The parens force the JS engine to recognize {}
as an object rather than a code block.
@EmNudge could you open a PR with a fix?
Fixed by #150
explanation behind that says it's just coercion of one type plus another. What is happening there is actually a code block and a unary + which coerces the array into 0. ({} + []) would get the same as ([] + {}) understandably.