This example demonstrates the scope of global and local variables.
Click on the page to display variable values.
View the source and then keep running it until you understand!
It is critical that you understand what
is going on.
Notice the ERROR. Why?????????????????
//The following is in the script block in the head...
//When you click the page, f1() and f2() are called.
var giX = 5
function f1() {
alert(giX)
var iZ = true
alert(iZ)
sQ = "I am global because var was not used ... BADBADBAD"
}
function f2() {
alert(giX)
alert(sQ)
alert(iZ)
}