Variable Scope Example

 

Behind the scene:

<script language = "JavaScript" >

var scope = "Global";

fn1();

fn2();

function fn1() {

document.write("<P> function <B>fn1</B> output:", "<BR>", "<BR>");

document.write("scope= ", scope, "<BR>", "<BR>"); // ==> global

var scope1 = "fn1 local";

}

function fn2() {

document.write("function <B>fn2</B> output", "<BR>", "<BR>")

document.write("scope= ",scope, "<BR>"); // ==> undefined, because the same variable is declared again inside fn2

var scope = "fn2 local";

document.write("scope= ",scope, "<BR>","<BR>","<BR>", "</P>"); // ==> fn2local

//document.write("scope1= ",scope1) // this would give a varible undefined JavaScript error.

}

</script>