Is JavaScript gaslighting me, or is there a logic to this?
I hit this line at about 1 a.m., laptop on a pillow, one eye open. I read the result, read it again, and seriously considered that the language was making fun of me personally.
This post takes that single line apart, one tiny piece at a time, until it stops being magic and starts being almost boring. That is a promise: boring is what we are aiming for.
The line that broke my brain
console . log ([] == ! []); // true
Enter fullscreen mode
Exit fullscreen mode
An empty array equals the opposite of an empty array. Sure. Of course it does. π
First, what does ![] even mean?
console . log ( ! []); // false
Enter fullscreen mode
Exit fullscreen mode
The ! flips a value to its opposite. Arrays count as "truthy" in JavaScript β even an empty one β so flipping an array gives you false .
So the scary line is really asking something simpler: is [] equal to false ?
Round two: [] against false
console .β¦