On this page

Global Execution Context

Every JavaScript program starts inside one special execution context. It is created before the first line of your code runs, and everything else happens inside it or on top of it. This is the Global Execution Context (GEC).

Definition

The Global Execution Context is the default, outermost execution context. JavaScript creates it once, when the program starts. Code that is not inside any function runs here.

Analogy: think of a building. The global execution context is the building itself. It exists before anyone enters. Each function call is a room that opens inside it for a while and closes again.

PropertyGlobal Execution Context
CreatedWhen the program starts
How manyExactly one per program
HoldsGlobal variables, global functions, the global object, and this
Position in the call stackAlways at the bottom
RemovedWhen the program ends (for a browser, when the tab closes)

Created in Two Phases

The GEC is built in two steps, like every execution context.

Phase 1: Creation (memory creation). JavaScript scans the code without running it, and sets things up:

  1. Creates the global object.
  2. Sets this to point to it.
  3. Allocates memory for variables and functions in the memory component.

Phase 2: Execution. JavaScript runs the code line by line and fills in the real values.

var n = 2;
function square(num) {
var result = num * num;
return result;
}
var square2 = square(n);
var square3 = square(3);

After the creation phase, the memory component looks like this, before any line has run:

KeyValue
nundefined
squarethe whole function code
square2undefined
square3undefined

Notice that functions are stored completely, while var variables are only a placeholder undefined. This is the reason hoisting works, and it is the topic of the next page.

During the execution phase:

  1. var n = 2; replaces undefined with 2.
  2. The function declaration is skipped, because it is already stored.
  3. var square2 = square(n); calls the function, which creates a new function execution context on top of the global one. When it returns 4, that context is removed, and square2 becomes 4.
  4. The same happens for square3, which becomes 9.

The Global Object

During the creation phase, JavaScript makes a global object. Its name depends on where the code runs:

EnvironmentGlobal object
Browserwindow (also self)
Node.jsglobal
Any environmentglobalThis (the standard name, works everywhere)

It holds the built-in features of the environment, such as setTimeout, console, Math, and parseInt, and in a browser also document, fetch, and localStorage. This is why you can call setTimeout(...) without defining it: JavaScript finds it on the global object.

this in the Global Context

At the global level, this depends on the environment:

Where the code runsthis at the top level
Browser, normal scriptThe window object
Browser, ES module (type="module")undefined
Node.js (CommonJS file)module.exports, an empty object {} at first
// In a normal browser script
console.log(this === window); // true
console.log(this === globalThis); // true

Global Variables and the Global Object

In a browser script, var and function declarations at the top level become properties of window. let and const do not.

var a = 1;
let b = 2;
const c = 3;
function hello() {}
console.log(window.a); // 1 (var becomes a property of window)
console.log(window.b); // undefined (let is global, but not on window)
console.log(window.c); // undefined
console.log(window.hello); // the function

This is another reason to avoid var: a global var can overwrite a built-in on window, or be overwritten by another script.

Node.js note: a Node file is wrapped in a module scope, so a top-level var there is not global. It stays private to the file.

Accidental Globals

If you assign to a name that was never declared, JavaScript (in the default, non-strict mode) creates a global variable for you:

function setPrice() {
price = 99; // no var, let, or const: becomes a global by accident
}
setPrice();
console.log(price); // 99 (leaked out of the function)

In strict mode ("use strict", and always inside ES modules and classes), this throws a ReferenceError instead, which is much safer.

The Global Context and the Call Stack

The call stack tracks which execution context is currently running. The GEC is pushed first and stays at the bottom until the program ends.

1While square(n) runs:
2
3| square() context | <- top (running now)
4| Global context | <- bottom (always here)

When square(n) returns, its context is popped off, and the global context continues with the next line.

Why Globals Cause Problems

Anything in the global scope is visible to all your code and to every script on the page. This leads to:

  • Name collisions. Two scripts both define var user, and one silently overwrites the other.
  • Hard-to-trace bugs. Any code can change a global, so you cannot tell who changed it.
  • No cleanup. Globals live for the whole life of the page.

How to avoid it:

  • Use const and let inside functions and blocks.
  • Use ES modules, so each file has its own scope.
  • Turn on strict mode, so accidental globals throw an error.

Summary

  • The Global Execution Context is created once, when the program starts, and holds code outside any function.
  • It is built in two phases: creation (global object, this, memory allocation) and execution (running line by line).
  • After creation, functions are stored whole and var variables are undefined.
  • The global object is window in a browser, global in Node, and globalThis everywhere.
  • At the top level, this is window in a browser script, undefined in an ES module, and module.exports in Node.
  • In a browser script, global var and functions become properties of window, but let and const do not.
  • It sits at the bottom of the call stack for the whole program.
  • Avoid globals: use let and const, modules, and strict mode.

Interview Answer: What Is the Global Execution Context?

The Global Execution Context is the default execution context that JavaScript creates once when a program starts. Code that is not inside a function runs in it.

It is created in two phases. In the creation phase, JavaScript creates the global object (window in a browser, global in Node), sets this to it, and allocates memory for variables and functions. Functions are stored with their full code, and var variables are set to undefined. In the execution phase, it runs the code line by line and assigns the real values. When a function is called, a new function execution context is pushed on top of the global one in the call stack.

In a browser script, a top-level var becomes a property of window, while let and const do not, which is one reason I avoid var and global variables, because they cause name collisions and hard-to-trace bugs.

The next topic is the creation phase in detail, which explains hoisting.

Next TopicFunction Execution Context