A stack stores elements so insertion and removal follow last-in, first-out order: the newest item is handled first. Operations happen at the top, like adding or removing dishes from a pile. JavaScript uses a context stack to manage function execution; the global context starts at the bottom, and each function call adds a frame. section 7.1
Function calls change the context stack in predictable ways:
- Invoking a function pauses the current context and activates a new function frame. section 7.1
- Returning from the function pops its frame, allowing the previous context to resume. section 7.1
- Nested functions, currying, and recursion grow the stack with additional frames. section 7.1
Recursive calls
In `function increment(i) { console.log(i); increment(++i); }`, every recursive call adds another frame. Because no call returns, the stack continues growing until the runtime halts. section 7.1
Figure 7.2. When invoking any function, like logger in this case, the single-threaded JavaScript runtime pauses the current global context and activates the context for the new function to run. At this point, a link is created between the global context and the function context, traversable via the scopeChain. Once logger returns, its execution context is popped off the stack, and the global context resumes.
Visualizes the global context pausing, a function context becoming active, and the function frame being popped on return. section 7.1













