variableGlobalA variable is a storage location paired with an associated name, which contains the information known as a value.  Nearly every programming language under the sun supports variables in some form or another.

Within the context of a single piece of code, the use of variables are straight forward: Store a value, retrieve a value, use the value.

var sum = 1 + 2;
console.log("The sum is " + sum);

The concept is nice and tidy within the context of a single code segment.  But not many software programs are a limited to a single instance.  With object oriented and modular programming where code logic is broken into smaller segments to enable code reuse and inheritance, sharing variables can be cumbersome.

Variable Scope

The concept of where variables are available and accessible is referred to as the variables context, or scope.  When you declare a variable, the section of code it is included in is referred to as local scope.  If you want to share a variable with different contexts (local scopes), you will need to use a global variable that lives within global scope.  Different languages use different constructs to get at global variables. 

Javascript will first look for a local scoped variable and if not found look for a global scoped one and use that.  The following example shows a global "sum" with various functions taking in as parameters,

var sum = 3;
console.log("SUM 1: " + sum);
function printSum2() {
  var sum = 5;
  console.log("SUM 2: " + sum);
}
printSum2();
console.log("SUM 2.5: " + sum);
function printSum3(sum) {
  sum = 6;
  console.log("SUM 3: " + sum);
}
printSum3(sum);
console.log("SUM 3.5: " + sum);
function printSum4(sum) {
  var sum = 7;
  console.log("SUM 4: " + sum);
}
printSum4(sum);
console.log("SUM 4.5: " + sum);

function printSum5() {
  sum = 9;
  console.log("SUM 5: " + sum);
}
printSum5();
console.log("SUM 5.5: " + sum);

Will result in the following output:

SUM 1  : 3
SUM 2  : 5
SUM 2.5: 3
SUM 3  : 6
SUM 3.5: 3
SUM 4  : 7
SUM 4.5: 3
SUM 5  : 9
SUM 5.5: 9

A problem arises when you have a local variable and a global variable with the same name and you wish to control which variable you are accessing.  Node.js has a solution for this with the "global" object.

The Global Object

Just as JavaScript in web browsers has the "window" object that is accessible to all code contexts as a global object, Node has a similar global-level object but is aptly named "global". 

global.sum = 3;
function printSum() {
  var sum = 5;
  console.log("Local Sum: " + sum);
  console.log("Global Sum: " + global.sum);
}

 

Local Sum: 5
Global Sum: 3

With the global object, you can initialize the variable in one context (function) and access it in another like the following:

function printSum() {
  global.sum = 3;
  console.log("Global Sum 1: " + global.sum);
}
function printSum2() {
  console.log("Global Sum 2: " + global.sum);
}
printSum();
printSum2();

 

Global Sum 1: 3
Global Sum 2: 3

There are semi-religious wars going on as to whether the global variables should never be used or are a necessary evil.  I fall in the later camp but do make sure you use them wisely and understand the context from which your variables are run to avoid debugging headaches later down the road.