JS: Global Variable

By Xah Lee. Date: . Last updated: .

For name declared with var:

For a variable name assigned but not declared, it's also global, called implied global. (see below)

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 System 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: Variable
  2. JS: let Declaration
  3. JS: const Declaration
  4. JS: var/function Declaration Order
  5. JS: var Name Scope
  6. JS: Global Variable

Function Topic

  1. JS: Define Function
  2. JS: Arrow Function
  3. JS: Function Parameters
  4. JS: var/function Declaration Order
  5. JS: var Name Scope
  6. JS: Function Declaration vs Function Expression
  7. JS: Closure
  8. JS: Functional Programing
  9. JS: Function Call, Apply, Bind
  10. JS: Function Argument Default Value
  11. JS: Function Rest Parameters
  12. JS: Function Argument Destructure

  1. JS: Function Object
  2. JS: Function.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

Ask me question on patreon