Scope (Definition)

The part of the script within which an identifier is reachable.

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.

Example code:

   // 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

Cross-references:

Wrox Instant JavaScript - page - 27