next up previous
Next: Comments Up: Variables Previous: Data Types

Scoping Rules

In JavaScript variables can be either local or global:

global
:variable is available to all parts of the program/
local
:

The following (meaningless) code shows the difference between global and local variables:

// declare the global variables
var global_one = 32;
var global_two;

function test {
  // this function has a local variable
  // a global value is copied into it
  // the local is then passed into a
  // function as a parameter

  var local_var; // local variable
  local_var = global_one; // copy global
                          // into local
  func_two(local_var);
  }

function func_two(number) {
  // create a local variable and copy
  // a parameter into it

  var local_var = number;
  }

// copying a local variable into a global
// like this is not allowed
global_two = local_var;



Dave Marshall
9/28/2001