Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question global variable problem

    Code:
    if(true){
    	var a = 2;//i thought 'a' is a local variable here.
    }
    alert(a);//but here it pops up '2',which means 'a' is a global variable?why?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Why do you think that a is global? If your code is within a function then a is local, and alert (a) results in the value of a being alerted.

    Code:
    <script type="text/javascript">
    
    test();
    
    var a = 10;
    function test() {
    var a = 12;  // note the var keyword defines a new variable local to the function.  But it is obviously silly to duplicate names in this way.
    alert ("Local " + a);  //12
    }
    alert ("Global " + a); //10
    
    </script>
    if(true) is meaningless as the condition will always be true.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 10-05-2012 at 08:51 AM.

    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.

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Why do you think that a is global? If your code is within a function then a is local, and alert (a) results in the value of a being alerted.

    Code:
    <script type="text/javascript">
    
    var a =10;
    function test() {
    var a = 12;
    alert ("Local " + a);  //12
    }
    alert ("Global " + a); //10
    
    test();
    
    </script>
    if(true) is meaningless as the condition will always be true.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    umm,but if a is not global,why the alert box shows '2'?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    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>
    Last edited by Philip M; 10-05-2012 at 09:08 AM.

    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.

  • Users who have thanked Philip M for this post:

    BaronZ (10-05-2012)

  • #5
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i understand the example u showed me.but in the code i showed above,i think 'a' is a local variable,so in my opinion,alert(a) should pop up 'undefined' or sth,but it's '2',that's what confused me.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by BaronZ View Post
    i understand the example u showed me.but in the code i showed above,i think 'a' is a local variable,so in my opinion,alert(a) should pop up 'undefined' or sth,but it's '2',that's what confused me.
    If your code fragment is not contained within a function then the variable a is global scope. Do what I say - study the topic.

    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.

  • Users who have thanked Philip M for this post:

    BaronZ (10-05-2012)

  • #7
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    If your code fragment is not contained within a function then the variable a is global scope. Do what I say - study the topic.
    ic now

  • #8
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    kinda different from java

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,238
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by BaronZ View Post
    kinda different from java
    Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •