The Global Execution Context
What you'll learn: You'll understand that the global execution context is the foundation of the call stack and stays there throughout your program's lifetime.
The Foundation of Every JavaScript Program
When your JavaScript program starts running, the very first thing that happens is the creation of the global execution context. This context sits at the bottom of the call stack—it's the bedrock upon which all other function calls are built.
Think of the call stack like a stack of cafeteria trays. The global execution context is the bottom tray that was placed there when the cafeteria opened. Every function call adds a new tray on top, and when each function finishes, its tray is removed. But that bottom tray? It stays there until the cafeteria closes (until your program terminates).
Why the Global Context Matters
The global execution context contains:
- All global variables and functions you've defined
- Built-in objects like
Array,Object, andMath - The global object itself (
windowin browsers,globalin Node.js)
Every time you call a function, a new stack frame is added on top of the global context. When that function returns, its frame is removed. But the global context remains, always at the bottom, always accessible.
When Does It Disappear?
The global execution context only leaves the call stack when:
- Your program finishes executing all code
- The browser tab/window closes
- Your Node.js process terminates
- An unrecoverable error crashes the program
Until one of those events occurs, it's the permanent foundation of your call stack.
Key Takeaway: The global execution context is the bottom layer of the call stack that remains for your program's entire lifetime, providing the foundation for all function calls and holding global variables and built-in objects.