Quote:
Originally Posted by BaronZ
umm,but if a is not global,why the alert box shows '2'?
|
I think you need to better understand global and local scope. You seem to be unclear. Study the example I have given you.
http://stackoverflow.com/questions/5...variable-scope
Code:
<script type="text/javascript">
test();
var a = 10;
function test(){
if(true) {
var a = 40;
}
alert (a); // within the function alerts '40', not the global value of '10'
}
alert (a); // outside the function alerts global value of '10'
</script>
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Last edited by Philip M; 10-05-2012 at 08:08 AM..
|