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 NodeJSglobal
.window
andglobal
are also global variables and have the value of an object. The object is known asglobal-object
, because any global variable is a property of theglobal-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 }