Previous Example-|-Next Example-|-Return to Chapter Listing

Example 4.4:
The Scope of Variables



We used the following scripts. First, we declared the variables and defined the functions in the HEAD: <!-- var variable1 = "Contents of variable1 OUTSIDE the functions" function funcExmpl1() { alert(variable1) } function funcExmpl2() { var variable1 = "Contents of variable1 INSIDE the function" alert(variable1) } function funcExmpl3() { alert(variable2) } //--> </SCRIPT> Then we created these buttons: <FORM> <INPUT TYPE="button" NAME="ButtonA" VALUE=" Show the global variable1 outside the functions " onclick="alert(variable1)"> <P> <INPUT TYPE="button" NAME="ButtonB" VALUE=" Show the global variable1 as seen from function funcExmpl1 " onclick="funcExmpl1()"> <P> <INPUT TYPE="button" NAME="ButtonC" VALUE=" Show the local variable1 as seen from function funcExmpl2 " onclick="funcExmpl2()"> <P> <INPUT TYPE="button" NAME="ButtonD" VALUE="Using a variable in a function where the variable not been declared" onclick="funcExmpl3()"> </FORM> Previous Example-|-Next Example-|-Return to Chapter Listing