An identifier is scoped to be globally available everywhere or locally available only within a function block.
However, JavaScript allows identifier names in local contexts to override those in the global context.
This means that an identifier name can be reused within a function and the local value will be used in place of the global value. Other global variables not overridden will be accessed from the global pool.
// Declare a global value
var myVariable = "Global Value";
// Declare a local value inside a function body
function local_scope()
{
var myVariable = "Local Value";
document.write(myVariable);
}
// Demonstrate the scope override local replacing global
document.write(myVariable);
local_scope();| See also: | __parent__, Scope chain, Variable |
| Prev | Home | Next |
| Scalar type | Up | Scope chain |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. | ||