Confused by Javascript's variable scope
<p>I'm finding myself by Javascript's variable scope, could someone explain to me why the first example doesn't work but the second does?</p>

<pre><code>function test() {
  return typeof(the_variable) !== 'undefined' &amp;&amp; the_variable;
}

(function () {
   var the_variable = "doesn't work";
   console.log(test());
}());

var the_variable = "does work";
console.log(test());
</code></pre>

<p>Output I get in my log:</p>

<pre><code>false
does work
</code></pre>

<p>Also I would like to know how to do something similar to the first example,</p>

<p>Cheers, Dave.</p>
