The Effects of Local and Global Variables : Variable Definition : Language Basics JAVASCRIPT DHTML TUTORIALS


JAVASCRIPT DHTML TUTORIALS » Language Basics » Variable Definition »

 

The Effects of Local and Global Variables




<html>
<head>
  <title>JavaScript Unleashed</title>
  <script type="text/javascript">
  <!--
    // Global variable modified in any function
    var numberA;
   
    // Global variable only modified in main script 
    var numberB;
   
    function doubleGlobalVar(){
      numberA *= 2;
    }
   
    function tripleLocalVar() {
      var numberA = 1
      numberA *= 3;
    }
   
    function doublePassedVar(numberB) {
      numberB *= 2;
    }
  //-->
  </script>
</head>
<body>
  <script type="text/javascript">  <!--
    numberA = 1;
    document.writeln("Initial value of numberA: " + numberA+"<br>");
    doubleGlobalVar();
    tripleLocalVar();
    document.writeln("Final value of numberA: " + numberA+"<br>");
    numberB = 1;
    document.writeln("Initial value of numberB: " + numberB+"<br>");
    doublePassedVar(numberB);
    document.writeln("Final value of numberB: " + numberB+"<br>");
  // -->
  </script>
</body>
</html>

           
       



-

Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

JAVASCRIPT DHTML TUTORIALS

 Navioo Language Basics
» Variable Definition