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 726261 of Variable

  • Revision slug: Glossary/Variable
  • Revision title: Variable
  • Revision id: 726261
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Editorial review

Revision Content

A variable has a name and value. The variable name is a placeholder where a value can stored.
 
  • In javascript, a variable value, or just called value, can be number, string, object, function or undefined. (NaN or Infinity is a special number, null is a special object)
  • Variable name is an identifier of at least one character or more using [_], [$], [A-Z], [a-z] or [0-9]. First character cannot be 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 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.
    
  • Variable name must belong to a context which can be the global space or a function, known as global variable or local variable respectively.  
  • Global space is the JavaScript code outside any function. 
  • A global variable can be accessed by any function.   
  • A local variable is local to the function within which the variable is declared. It can only be accessed within that function. It will not be accessible outside that function which means by other functions or by global space code.  
  • 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();
  • The global space has a name. In browser, it is named as window. In NodeJS, it is global. Actually, these names, window or global are also global variables and have the value of an object. The object is known as 'global-object', because any global variable will be 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 due to '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 due to '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

<div>A variable has a name and value. The variable name is a placeholder where a value can stored.</div>

<div>&nbsp;</div>

<ul>
 <li>In javascript, a variable value, or just called value, can be number, string, object, function or undefined. (NaN or Infinity is a special number, null is a special object)</li>
 <li>Variable name is an identifier of at least one character or more using [_], [$], [A-Z], [a-z] or [0-9]. First character cannot be digit. Example:</li>
 <li>
  <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 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>
 </li>
</ul>

<ul>
 <li>Variable name must belong to a context which can be the global space or a function, known as global variable or local variable respectively. &nbsp;</li>
 <li>Global space is the JavaScript code outside any function.&nbsp;</li>
 <li>A global variable can be accessed by any function. &nbsp;&nbsp;</li>
 <li>A local variable is local to the function within which the variable is declared. It can only be accessed within that function. It will not be accessible outside that function which means by other functions or by global space code. &nbsp;</li>
 <li>Example:</li>
</ul>

<pre style="margin-left: 40px;">
<span style="line-height: 1.5;">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
</span>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. &nbsp;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() {
    <span style="font-size: 1rem; word-spacing: normal;">c = "x"    // create a global variable-c.
    "</span><span style="font-size: 1rem; word-spacing: normal;">use strict";</span>
<span style="font-size: 1rem; word-spacing: normal;">    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();</span></pre>

<ul>
 <li>The global space has a name. In browser, it is named as&nbsp;<u>window</u>. In NodeJS, it is <u>global</u>. Actually, these names, <u>window </u>or <u>global </u>are also global variables and have the value of an object. The object is known as 'global-object', because any global variable will be a property of the 'global-object'. &nbsp;See example below:</li>
</ul>

<pre style="margin-left: 40px;">
// file: a.js
<span style="line-height: 1.5;">b = 1;    // 'b' will be global variable.  It will also be a property of the 'global-object'.
</span>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 due to '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 due to '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"><span style="line-height: 1.5;">notes to NodeJS:&nbsp;</span></h3>

<p style="margin-left: 40px;"><span style="line-height: 1.5;">File a.js has 2 lines:</span></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