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.
| Property | Global Execution Context |
|---|---|
| Created | When the program starts |
| How many | Exactly one per program |
| Holds | Global variables, global functions, the global object, and this |
| Position in the call stack | Always at the bottom |
| Removed | When 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:
- Creates the global object.
- Sets
thisto point to it. - 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:
| Key | Value |
|---|---|
n | undefined |
square | the whole function code |
square2 | undefined |
square3 | undefined |
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:
var n = 2;replacesundefinedwith2.- The function declaration is skipped, because it is already stored.
var square2 = square(n);calls the function, which creates a new function execution context on top of the global one. When it returns4, that context is removed, andsquare2becomes4.- The same happens for
square3, which becomes9.
The Global Object
During the creation phase, JavaScript makes a global object. Its name depends on where the code runs:
| Environment | Global object |
|---|---|
| Browser | window (also self) |
| Node.js | global |
| Any environment | globalThis (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 runs | this at the top level |
|---|---|
| Browser, normal script | The window object |
Browser, ES module (type="module") | undefined |
| Node.js (CommonJS file) | module.exports, an empty object {} at first |
// In a normal browser scriptconsole.log(this === window); // trueconsole.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); // undefinedconsole.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
varthere 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
constandletinside 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
varvariables areundefined. - The global object is
windowin a browser,globalin Node, andglobalThiseverywhere. - At the top level,
thisiswindowin a browser script,undefinedin an ES module, andmodule.exportsin Node. - In a browser script, global
varand functions become properties ofwindow, butletandconstdo not. - It sits at the bottom of the call stack for the whole program.
- Avoid globals: use
letandconst, 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.