I recently came across the site wtfjs.com. It highlights some idiosyncrasies of the Javascript language. It’s worth a look, you’ll get a deeper understanding of the Javascript language.
This entry submitted by @diogobaeder held my attention. The solution was not immediately obvious to me so I did some digging. Once I found the correct type coercion rules the result were logical.
var foo = [0];
console.log(foo == foo); // true
console.log(foo == !foo); // true - ???
Let’s examine this step by step.
foo == foo
The above was obvious. References to the same object evaluate to true.
foo == !foo
The expression above took a little longer to decipher.
First the precedence rules come into play.
![0]; // conversion to Boolean(), false
[0] == false; // true
If either operand is a number or a boolean, the operands are converted to numbers.
Number(false) == 0;
Number([0]) == 0;
0 == 0;