logo

Execution Context

How functions and code blocks create execution contexts, and the role of the call stack.

Short explanation

An execution context is the environment in which code is evaluated β€” it contains variable bindings, the this value, and the scope chain. The call stack keeps track of active execution contexts.

Small snippet

function a() {
  b();
}
function b() {
  console.log('hello');
}
a();
// Call stack: global -> a -> b

How JS handles it internally

When a function is invoked, a new execution context is pushed onto the call stack. When it returns, the context is popped. This model explains stack traces, recursion limits, and how this is resolved.

FAQ

Q: What happens if the stack overflows?

A: A stack overflow occurs with unbounded recursion and typically causes the runtime to throw an error (or freeze).