The Effects of Local and Global Variables
<html>
<head>
<title>JavaScript Unleashed</title>
<script type="text/javascript">
<!--
// Global variable modified in any function
var numberA;
// Global variable only modified in main script
var numberB;
function doubleGlobalVar(){
numberA *= 2;
}
function tripleLocalVar() {
var numberA = 1;
numberA *= 3;
}
function doublePassedVar(numberB) {
numberB *= 2;
}
//-->
</script>
</head>
<body>
<script type="text/javascript"> <!--
numberA = 1;
document.writeln("Initial value of numberA: " + numberA+"<br>");
doubleGlobalVar();
tripleLocalVar();
document.writeln("Final value of numberA: " + numberA+"<br>");
numberB = 1;
document.writeln("Initial value of numberB: " + numberB+"<br>");
doublePassedVar(numberB);
document.writeln("Final value of numberB: " + numberB+"<br>");
// -->
</script>
</body>
</html>
Related examples in the same category