Global Execution Context vs Function Execution Context
JavaScript creates a global context once and a function context on every call. Here is how they differ.
Global Execution Context vs Function Execution Context
JavaScript creates execution contexts for code to run in. The two main types are global and function. They look similar but behave differently.
Global Execution Context (GEC)
- Created once when the script starts.
- There is exactly one GEC per program.
- In browsers,
thispoints to thewindowobject. - Variables declared with
varat the top level become properties ofwindow. - Stays alive until the program (or the browser tab) is closed.
Function Execution Context (FEC)
- Created every time a function is invoked.
- One per function call. Recursive calls create nested FECs.
thisdepends on how the function was called: regular call, method call, call/apply/bind, or arrow function.- Variables declared with
varare local. They do not leak to the global object. - Destroyed when the function returns.
How They Interact
Every FEC is pushed onto the call stack. The GEC sits at the bottom of the stack. Each function call pushes a new frame; each return pops one. When the GEC is the only frame left, the program is idle (waiting for events).
Example
var a = 1; // lives in GEC function outer() { var b = 2; // lives in outer's FEC function inner() { var c = 3; // lives in inner's FEC console.log(a + b + c); // 6, via scope chain } inner(); } outer();
At the moment inner runs, the stack from bottom to top is: GEC, outer, inner.
The Takeaway
The GEC is created once, holds global vars and this = window. An FEC is created per function call, holds local vars, and is destroyed on return. The call stack tracks which context is currently running.
The global context is created once at program start and lives until the program ends. A function context is created every time a function is called and is destroyed when it returns.
Exactly one per program. It sits at the bottom of the call stack for the entire lifetime of the script.
In a browser, this points to the window object. In Node.js modules, this points to module.exports (or globalThis in modern code).
No. Variables declared with var inside a function are local to that function's execution context and are not attached to window or globalThis.
When the function returns. Its frame is popped from the call stack and its local variables become eligible for garbage collection unless captured by a closure.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

