Local and global variables in javascript
<p>Today,I started to code a page that prompt the user to choose their PC spec,and the code is as follow</p>

<pre><code>&lt;html&gt;
&lt;title&gt;Computer Specification Chooser&lt;/title&gt;

&lt;head&gt;

&lt;script type="text/javascript"&gt;

var compSpec = document.compChooser;

function processorUnavailable_onclick()
{
 alert("Sorry that processor speed is currently unavailable");
 compSpec.processor[2].checked = true;

}

&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;form name="compChooser"&gt;

&lt;p&gt;Tick all components you wan included on your computer&lt;/p&gt;

&lt;p&gt;
DVD-ROM

&lt;input type="checkbox" name="chkDVD" value="DVD-ROM" /&gt;
&lt;br /&gt;

CD-ROM
&lt;input type="checkbox" name="chkCD" value="CD-ROM" /&gt;
&lt;br /&gt;

Zip Drive
&lt;input type="checkbox" name="chkZIP" value="ZIP DRIVE" /&gt;
&lt;/p&gt;

&lt;p&gt;
Select the processor speed you require
&lt;br /&gt;

&lt;input type="radio" name="processor" value="3.8" /&gt;
3.8 GHZ

&lt;input type="radio" name="processor" value="4.8" onclick="processorUnavailable_onclick()" /&gt;
4.8 GHZ


&lt;input type="radio" name="processor" value="6" /&gt;
6 GHZ
&lt;/p&gt;

&lt;input type="button" name="btnCheck" value="Check Form" /&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>The problem I'm facing is on the function that I've tie to the event handler,when I try to choose the radio button of the processor value 4.8 GHZ,yes it alert me with the message inside the function,but after that,it doest not execute the next statement inside the function,that is to check the next processor value 6 GHZ.</p>

<p>I've try my effort to change it and test on it,and find out when I set the <code>var compSpec = document.compChooser</code> as a local variable inside the function instead of a global variable,the next statement could be executed.</p>

<p>But I thought for a global variable,it is accessible in everywhere on the page and also inside a function.But why now I can't accesses it inside my function??Any idea??</p>

<p>Besides,I stumble upon a weird article while googling,it says that when a global variable is created,it is added to window object.I just curious why this happen??And what's the benefits and uses of it??</p>

<p>THANK YOU</p>
