Why does 2 == [2] in JavaScript?
<p>I recently discovered that <code>2 == [2]</code> in JavaScript.  As it turns out, this quirk has a couple of interesting consequences:</p>

<pre><code>var a = [0, 1, 2, 3];
a[[2]] === a[2]; // this is true
</code></pre>

<p>Similarly, the following works:</p>

<pre><code>var a = { "abc" : 1 };
a[["abc"]] === a["abc"]; // this is also true
</code></pre>

<p>Even stranger still, this works as well:</p>

<pre><code>[[[[[[[2]]]]]]] == 2; // this is true too! WTF?
</code></pre>

<p>These behaviors seem consistent across all browsers.</p>

<p>Any idea why this is a language feature?</p>

<p><strong>EDIT</strong>: Here are more insane consequences of this "feature":</p>

<pre><code>[0] == false // true
if ([0]) { /* executes */ } // [0] is both true and false!

var a = [0];
a == a // true
a == !a // also true, WTF?
</code></pre>

<p>These examples were found by jimbojw of <a href="http://www.trephine.org" rel="nofollow">trephine</a> and <a href="http://jimbojw.com" rel="nofollow">http://jimbojw.com</a> fame as well as <a href="http://thefoley.net/" rel="nofollow">walkingeyerobot</a>.</p>
