 <p>Simply because b variable is initialized inside the function print.. and c variable is initialised on the global scope. so at this line: </p> <pre><code>document.write("b is : " + b + "&lt;br /&gt;"); </code></pre> <p>you trying to get the value of b variable but ur not calling the function print..</p> <p>look at this :</p> <pre><code>var a = 45; var b; var c; function print(){ b = 10; document.write(a); } if(a == 45) { var c = 10; } document.write("a is : " + a + "&lt;br /&gt;"); document.write("b is : " + b + "&lt;br /&gt;"); document.write("c is : " + c + "&lt;br /&gt;"); //--&gt; </code></pre> <p>it will print the following:</p> <pre><code> a is : 45 b is : undefined c is : 10 </code></pre> <p>because b didn`t change as the function has not yet been called .. but c has changed by the if statement which lies on the global scope.</p> <pre><code>var a = 45; var c; function print() { b = 10; document.write(a); } print() if (a == 45) { var c = 10; } document.write("a is : " + a + "&lt;br /&gt;"); document.write("b is : " + b + "&lt;br /&gt;"); document.write("c is : " + c + "&lt;br /&gt;"); //--&gt; </code></pre>