Global and Local Variable Scope Demonstration : Variable Definition « Language Basics « JavaScript DHTML






Global and Local Variable Scope Demonstration

  

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
var globalVar = "Boy"   // global
var globalVar2 = "dog"        // global
function demo() {
    var globalVar2 = "cat"    // local version of globalVar2
    var output = globalVar2 + " does not belong to " + globalVar + ".<BR>"
    document.write(output)
}
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
demo();
document.write(globalVar2 + " belongs to " + globalVar + ".")
</SCRIPT>
</BODY>
</HTML>


           
         
    
  








Related examples in the same category

1.The Effects of Local and Global Variables
2.Use of Global and Local Variables
3.Event Handler with Multiple Statements in Attribute Value
4.Global Versus Local Scope of a Variable
5.Variable scope
6.Variable scoping
7.Get the type of a variable
8.Global scope and page scope
9.String value is passed by value, while the array is passed by reference