Scope and the Function (Call) Stack
In short: every function call pushes a frame onto the call stack — a record of where to resume once that call finishes. Frames stack like plates: the newest call sits on top, and it must finish (be popped) before the call underneath it can continue. A function's own variables live only in its own frame — that's scope — and a function that calls itself without ever stopping just keeps pushing frames until the stack overflows.

Watching the stack build up
Every nested call adds a plate; every return removes one:
main() is pushed first. It calls total(cart), pushed on top. total
calls gstRate once per item, each pushed and popped in turn before
total can finish its reduce and return its own value to main. At no
point does a lower frame continue before the one above it is done.
Recursion: a function calling itself, on purpose
A function calling itself is normal — as long as it has a way to stop:
Each call to countdown pushes a new frame with its own n. n <= 0 is
the base case — the condition that stops the recursion and lets every
pushed frame finally pop, one by one, back down to the original call.
Recursion with no base case overflows the stack
Remove the stopping condition, and the stack has nowhere to stop growing:
Every call immediately calls another before it can return, so no frame
ever pops. The stack grows until it hits the engine's fixed size limit,
and JavaScript throws RangeError: Maximum call stack size exceeded — the
practical proof that the call stack is a real, limited structure, not an
infinite list.
Quick practice
When function A calls function B, which frame is on top of the call stack while B is running?
What must a recursive function have to avoid a stack overflow?
What error does JavaScript throw when the call stack grows too large?
