Return to Kroll Design home page

JavaScript Tutorials

 
« Return

local variables

See also:

If you declare a variable within a function using the "var" keyword, that variable is "local" to the function, which means that nothing knows of this variable outside the function, even if there exists a variable outside the function with the same variable name. In the following example, there's a variable named foo, and another variable inside the function, also named foo. Even though they have the same variable name, these are two separate variables:

Code:

<script type="text/javascript">
//<![CDATA[
var foo = 3; //this is global

function local_variable_test() {
  var foo = 7; // this is local
  document.write("The value of the function's foo is: ", foo, "<br>");
}

local_variable_test();

document.write("The value of the global foo is: ", foo);
//]]>
</script>

Output:



 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM