Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 750039 of Variable

  • Revision slug: Glossary/Variable
  • Revision title: Variable
  • Revision id: 750039
  • Created:
  • Creator: klez
  • Is current revision? No
  • Comment removed NeedsContent tag

Revision Content

A variable is a named location for storing a {{Glossary("Value", "value")}}. That way an unpredictable value can be accessed through a predetermined name.

  • {{Glossary("JavaScript")}} variables can hold these types of values: number, string, object, function, or undefined. (NaN or Infinity is a special number, null is a special object)
  • A variable name is an identifier of one or more alphanumeric characters. The underscore and dollar sign are also allowed, but the first character cannot be a digit. Example:
var a, _, $, _0, $_, a1, a_, __, $$, __xx__; // good name
var #, 0a, %1, a&, a*, 1ab;   // bad
var a.a;                      // bad

var a = {a:1};    // good.  The first 'a' is a variable name having a value of object.  
                  //        Second 'a' is not called as a variable name, but called as property name of the object.
console.log(a.a)  // good.  First 'a' is a variable name have a value of object.  Second 'a' is the property name of the object-a.
  • Depending on the context, a variable can be global or local:
global variable
belongs to the global space. Global variables can be accessed by any {{Glossary("Function", "function")}}.
local variable
belongs to the function in which the variable is declared, and can be accessed only within that function, not from other functions or from the global space.
global space
the JavaScript code outside all functions

Example:

var a = 1;    // 'a' will be global variable in browser, but a local variable in NodeJS (see notes to NodeJS)
b = 'a';      // 'b' will be global variable both in browser and NodeJS
function f1() {
  var a = 2;             // 'a' will be a local variable.  It hides the global variable-a.
  console.log(a);        // 2   accessing the local variable-a
  console.log(b);        // 'a' accessing the global variable-b
  var z = 'zzz';         // any variable declared within a function will never be accessed outside the function.
}
function f2() {
  var a = 3;             // local variable
  console.log(a)         // 3
  console.log(b)         // 'a'
  // Within any other function, there is no way to access the var z declared in f1()
}
f1();                    // calling function-f1
f2();                    // calling function-f2
console.log(f1.z);       // undefined.  The var-z declared also can never be accessed by code in the global space (not embedded by any function).
                         //             'f1.z' is not the coding to access the var-z in f1(). 'f1.z' has other meaning.
f1.z = 'zz??';           // 'f1.z' means a property of the function-f1.  
                         // It is OK to attach a property-z to any function like this.
console.log(f1.z);       // 'zz??'.   'f1' is a function and 'z' is its property created at previous line.   
  • Assigning a value to an undeclared variable within a function will create a global variable. However, if the function has a line "use strict", it will throw an exception:
"use strict";  // "use strict" has no effect outside a function.
var a = "x";   // create a local variable-a.
b = "x";       // create a global variable-b.
function f1() {
    c = "x"    // create a global variable-c.
    "use strict";
    d = "x";   // throw exception
}
function f2() {
    e = "x"; // global variable-e is created.  The "use strict" in global space or f1() will not have an effect here.
}
f1();
f2();
  • In the browser, the global space is called window, in NodeJS global. window and global are also global variables and have the value of an object. The object is known as global-object, because any global variable is a property of the global-object.  See example below:
// file: a.js
b = 1;    // 'b' will be global variable.  It will also be a property of the 'global-object'.
function f1() {
  var b = 2;             // 'b' will be a local variable
  console.log(b)         // 2
  console.log(window.b)  // 1 in browser, but exception in NodeJS since 'window' is undefined.   
                         // 'window' is the variable name that has a value of an object.
                         // or, in short, 'window' is an object.  
                         // 'b' is a property of 'window'
  console.log(global.b)  // 1 in NodeJS, but exception in browser since 'global' is undefined.
                         // 'global' is an object in NodeJS.
}
f1();          
console.log(window.window === window) // true in browser, but exception in NodeJS.  
                                      // 'window.window' and 'window' are 
                                      // both pointing to the same value, 
                                      // which is the 'global-object'.

console.log(global.global === global) // true in NodeJS, but exception in browser.

notes to NodeJS:

File a.js has 2 lines:

var a = 1; // 'a' will be a local variable in NodeJS, but a global variable in browser.
b = 'a';   // 'b' will be global variable in both NodeJS and browser.
c:\>node a.js 

nodeJS will always embed the running *.js under a function of 5 parameters:

function(exports, require, module, __dirname, __filename) {
    var a = 1;    // local variable
    b = 'a';      // global variable
}

Revision Source

<p>A variable is a named location for storing a {{Glossary("Value", "value")}}. That way an unpredictable value can be accessed through a predetermined name.</p>

<ul>
 <li>{{Glossary("JavaScript")}} variables can hold these types of values: number, string, object, function, or undefined. (NaN or Infinity is a special number, null is a special object)</li>
 <li>A variable name is an identifier of one or more alphanumeric characters. The underscore and dollar sign are also allowed, but the first character cannot be a digit. Example:</li>
</ul>

<pre>
var a, _, $, _0, $_, a1, a_, __, $$, __xx__; // good name
var #, 0a, %1, a&amp;, a*, 1ab;   // bad
var a.a;                      // bad

var a = {a:1};    // good.  The first 'a' is a variable name having a value of object.  
                  //        Second 'a' is not called as a variable name, but called as property name of the object.
console.log(a.a)  // good.  First 'a' is a variable name have a value of object.  Second 'a' is the property name of the object-a.
</pre>

<ul>
 <li>Depending on the context, a variable can be global or local:</li>
</ul>

<dl>
 <dt>global variable</dt>
 <dd>belongs to the global space. Global variables can be accessed by any {{Glossary("Function", "function")}}.</dd>
 <dt>local variable</dt>
 <dd>belongs to the function in which the variable is declared, and can be accessed only within that function, not from other functions or from the global space.</dd>
 <dt>global space</dt>
 <dd>the JavaScript code outside all functions</dd>
</dl>

<p>Example:</p>

<pre style="margin-left: 40px;">
var a = 1;    // 'a' will be global variable in browser, but a local variable in NodeJS (see notes to NodeJS)
b = 'a';      // 'b' will be global variable both in browser and NodeJS
function f1() {
  var a = 2;             // 'a' will be a local variable.  It hides the global variable-a.
  console.log(a);        // 2   accessing the local variable-a
  console.log(b);        // 'a' accessing the global variable-b
  var z = 'zzz';         // any variable declared within a function will never be accessed outside the function.
}
function f2() {
  var a = 3;             // local variable
  console.log(a)         // 3
  console.log(b)         // 'a'
  // Within any other function, there is no way to access the var z declared in f1()
}
f1();                    // calling function-f1
f2();                    // calling function-f2
console.log(f1.z);       // undefined.  The var-z declared also can never be accessed by code in the global space (not embedded by any function).
                         //             'f1.z' is not the coding to access the var-z in f1(). 'f1.z' has other meaning.
f1.z = 'zz??';           // 'f1.z' means a property of the function-f1.  
                         // It is OK to attach a property-z to any function like this.
console.log(f1.z);       // 'zz??'.   'f1' is a function and 'z' is its property created at previous line.   </pre>

<ul>
 <li>Assigning a value to an undeclared variable within a function will create a global variable. However, if the function has a line "use strict", it will throw an exception:</li>
</ul>

<pre style="margin-left: 40px; font-size: 14px; word-spacing: 0px;">
"use strict";  // "use strict" has no effect outside a function.
var a = "x";   // create a local variable-a.
b = "x";       // create a global variable-b.
function f1() {
    c = "x"    // create a global variable-c.
    "use strict";
    d = "x";   // throw exception
}
function f2() {
    e = "x"; // global variable-e is created.  The "use strict" in global space or f1() will not have an effect here.
}
f1();
f2();</pre>

<ul>
 <li>In the browser, the global space is called <code>window</code>, in NodeJS <code>global</code>. <code>window</code> and <code>global</code> are also global variables and have the value of an object. The object is known as <code>global-object</code>, because any global variable is a property of the <code>global-object</code>. &nbsp;See example below:</li>
</ul>

<pre style="margin-left: 40px;">
// file: a.js
b = 1;    // 'b' will be global variable.  It will also be a property of the 'global-object'.
function f1() {
  var b = 2;             // 'b' will be a local variable
  console.log(b)         // 2
  console.log(window.b)  // 1 in browser, but exception in NodeJS since 'window' is undefined.   
                         // 'window' is the variable name that has a value of an object.
                         // or, in short, 'window' is an object.  
                         // 'b' is a property of 'window'
  console.log(global.b)  // 1 in NodeJS, but exception in browser since 'global' is undefined.
                         // 'global' is an object in NodeJS.
}
f1();          
console.log(window.window === window) // true in browser, but exception in NodeJS.  
                                      // 'window.window' and 'window' are 
                                      // both pointing to the same value, 
                                      // which is the 'global-object'.

console.log(global.global === global) // true in NodeJS, but exception in browser.</pre>

<h3 id="notes_to_NodeJS.3A">notes to NodeJS:</h3>

<p style="margin-left: 40px;">File a.js has 2 lines:</p>

<pre style="margin-left: 40px;">
var a = 1; // 'a' will be a local variable in NodeJS, but a global variable in browser.
b = 'a';   // 'b' will be global variable in both NodeJS and browser.
</pre>

<pre style="margin-left: 40px;">
c:\&gt;node a.js </pre>

<p style="margin-left: 40px;">nodeJS will always embed the running *.js under a function of 5 parameters:</p>

<pre style="margin-left: 40px;">
function(exports, require, module, __dirname, __filename) {
    var a = 1;    // local variable
    b = 'a';      // global variable
}</pre>
Revert to this revision