| Refresh | Home EGTry.com

scope of variable, global or local


commentsJavascript sourceExecution result in your current browser
define a global, and use it inside function body
   var a=1;
   function f1() {
    document.write("a="+a);
   }
   f1();

modify inside function
   function f2() {
     a=a*2; //refer to the same global a
     document.write("inside function a="+a);
   }
   f2();
   document.write("<br/>outside function a="+a);

redefine the same name variable inside function
    function defineA() {
      var a=100; //use the own copy
      document.write("inside function a="+a);
    }
    defineA();
    document.write("<br/> outside function a="+a);

define a function that use a global variable. then change the variable, can call the function. old value or new value?
     function useGlobal() {
       document.write("a="+a);
     }
     a=124;
    useGlobal();

define a variable inside function. can be used outside the function?
      function defineVar() {
        var b=233;
      }
      document.write("b="+b);
 

unlike C and Java, curly brace does not introduce another scope
      var c=12;
      {
         var c=23; 
      }
      document.write("c="+c);