Facebook Pixel

JavaScript Function Invocation and the Call Stack

Calling a function is more than running code. Here is the full sequence from invocation to return.

JavaScript Function Invocation and the Call Stack

When you invoke a function in JS, several things happen in a precise order. Understanding the sequence explains variable resolution, the this keyword, and async timing.

Step 1: Invocation

A function is invoked by appending () to a reference, or via call/apply/bind. Arrow functions and regular functions both follow the same call stack rules.

Step 2: Create the Execution Context

The engine creates a new function execution context with:

  • A variable environment for locals.
  • An outer environment reference (lexically determined at write time).
  • A this binding (determined at call time).

Step 3: Memory Allocation Phase

The engine scans the function body. var locals get undefined. Inner function declarations are stored in full. let and const are placed in the Temporal Dead Zone.

Step 4: Push onto the Call Stack

A new frame is pushed on top of the stack. The frame contains the arguments object, local variables, and the return address.

Step 5: Code Execution Phase

The engine runs the body line-by-line. Variables are assigned, nested functions are invoked (recursively pushing new frames), and async operations are scheduled.

Step 6: Return and Pop

When the function hits a return statement or reaches the end, the frame is popped. The return value (or undefined) is passed to the caller. Local variables are destroyed unless captured by a closure.

Async Wrinkle

If the function schedules async work (a setTimeout callback), the callback is a separate function invocation. Its execution context is created later, when the event loop pushes it onto the (then-empty) call stack.

The Takeaway

Invocation creates an execution context, runs the memory phase, pushes a frame onto the call stack, executes the body, then pops the frame on return. Async callbacks are separate invocations that happen later, when the stack is free.

An execution context is created, memory is allocated for locals, a frame is pushed onto the call stack, the body runs line-by-line, and the frame is popped on return. The return value is passed to the caller.

It immediately pops the current function's frame from the call stack. The return value is handed to the calling function, which resumes at the next statement.

At invocation (except for arrow functions). The way you call the function (plain, method, call, apply, bind, new) determines this. Arrow functions inherit this from their lexical scope at definition time.

They are destroyed with the popped stack frame, unless a closure is holding a reference to the variable environment, in which case they persist.

When the event loop pushes the callback onto the call stack, not when setTimeout is called. The original function may have returned long before.

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.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.