Why Use VAR To Define Varibles

[Total: 0    Average: 0/5]

Creating a variable with the VAR command can impact what the scope of the varialble will be.

Body Code:

<script type=text/javascript>
var scope1 = "(var) 1 global scope";
function checkscope1() {
  var scope1 = "(var) 1 local scope<BR>";
  document.writeln(scope1);
}
document.writeln("before="+scope1+"<br>");
checkscope1();
document.writeln("after="+scope1);

document.writeln("<BR><HR width=50%><BR>");

scope2 = "(no var) 2 global scope";
function checkscope2() {
  scope2 = "(no var) 2 local scope<br>";
  document.writeln(scope2);
}
document.writeln("before="+scope2+"<br>");
checkscope2();
document.writeln("after="+scope2);
</script>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *