View Single Post
Old 10-05-2012, 07:57 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by BaronZ View Post
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..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
BaronZ (10-05-2012)