JS: Global Variable

By Xah Lee. Date: . Last updated: .

Global variable is a variable whose scope is the whole source file it is in. That is, it has meaning anywhere in the source file.

Global variable can be created by:

[see JS: let Declaration]

[see JS: var Name Scope]

For a variable name assigned but not declared, it's called implied global.

Every global variable name is also a global property name, and vice versa.

First, here's clarification of terms:

Name declaration and assingment:

  1. declared and assigned → good.
  2. declared and unassigned → has default value of undefined.
  3. undeclared and assigned → implied global variable. (not exactly the same as declared global variable. Their configurable attribute's value is different.)
  4. undeclared and unassigned (and accessed) → ReferenceError.

Global Variable = Property of Global Object

Global variable is just property of the global object. [JS: the Global Object]

In web browser, the global object is window.

[see JS: the Global Object]

// global variable is a property of global object
var xx = 3;
console.log(window.xx); // 3

(copy and paste the code and try it in your browser's JavaScript console. [see JS: How to Use Browser Console])

// global variable is a property of global object
window.yy = 4;
console.log(yy); // 4

[see JS: Object Overview]

[see JS: Check Property Existence]

Undeclared Variable

When a variable is assigned but not declared, it is a global variable. This is sometimes called “implied global”.

// example of undeclared variable

function f () { x = 4; return 1; }
f();

// this is a global variable
console.log(x); // 4

// it's a property of the global object
// in browser, the global object is “window”
console.log(window.x); // 4

// in node.js, the global object is “global”
console.log(global.x); // 4

Difference Between Implied Global and True Global Variable

Property's configurable attribute means whether the property can be deleted (For example, delete window.x) and whether its attributes can be changed. [see JS: Property Attributes, writable, enumerable, configurable]

// difference between declared and undeclared variable

var x = 1;
console.log(Object.getOwnPropertyDescriptor(window, "x"));
// Object {value: 1, writable: true, enumerable: true, configurable: false}

y = 1; // undeclared
console.log(Object.getOwnPropertyDescriptor(window, "y"));
// Object {value: 1, writable: true, enumerable: true, configurable: true}

(Note: as of , in Firefox, if you run code in JavaScript console, undeclared var's configurable property is the same as declared global variable (presumably for programer convenience). But in Google Chrome's JavaScript console, it behaves by spec.)

Accessing Undeclared and Unassigned Variable → ReferenceError

x; // ReferenceError: x is not defined

var y; // unassigned var has value of 「undefined」
console.log(y);           // prints undefined

JS Variable Topic

  1. JS: let Declaration
  2. JS: const Declaration
  3. JS: var Declaration
  4. JS: var Declaration Order
  5. JS: var Name Scope
  6. JS: Global Variable
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.