Boolean operators in if statement not working
<p>Could someone explain what's happening here? I'm trying to use the javascript <code>!!</code> (double-bang) operator <a href="http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript">as described here</a> in conjunction with <a href="http://diveintohtml5.info/storage.html" rel="nofollow">HTML5 local storage</a> (I'm storing 0 and 1 values and testing for truthiness, but I also need a missing key to be false, hence the undefined at the start.</p>

<p>Although it echos to the console as false when typecast, it doesn't in the 'if' statement.</p>

<pre><code>var foo = undefined;

// outputs undefined
console.log(foo)

// typecast to non-inverted boolean 
console.log(!!foo);

if (!!foo) {
    console.log("If was false before, why won't this execute?");
}​ else {
    console.log("It didn't work");
}​​​​​​​​​​​​​​​
</code></pre>

<p>Produces:</p>

<pre><code>undefined
false
It didn't work 
</code></pre>

<p><a href="http://jsfiddle.net/YAAA7/" rel="nofollow">http://jsfiddle.net/YAAA7/</a><br>
(Chrome v 23.0.1271.97 &amp; Firefox 16.0.1, OS X 10.8.2)</p>

<p><strong>Edit - corrected code:</strong></p>

<p>(The previous 'if' statement was just evaluating as false, so that branch would never run.)</p>

<pre><code>var foo = false;

// outputs undefined
console.log(foo)

// typecast to non-inverted boolean 
console.log(!!foo);

if (!!foo == false) {
    console.log("Matches for foo undefined, foo = 0 and foo = false");
} else {
    console.log("Matches for foo = 1 and foo = true");
}​
</code></pre>

<p><a href="http://jsfiddle.net/YAAA7/1/" rel="nofollow">http://jsfiddle.net/YAAA7/1/</a></p>
