Function Execution Context
Each time you call a function, JavaScript creates a brand-new execution context just for that call. This is the Function Execution Context (FEC).
Definition
A Function Execution Context is the execution context created every time a function is called. It holds that function's parameters, local variables, and inner functions, and it is removed when the function returns.
Analogy: if the global execution context is the building, each function call is a room that opens inside it. The room has its own storage and its own work going on, and it closes when the work is done.
| Property | Function Execution Context |
|---|---|
| Created | Every time a function is called (not when it is defined) |
| How many | One per call (calling a function 3 times creates 3 contexts) |
| Holds | Parameters, the arguments object, local variables, inner functions, and this |
| Position in the call stack | Pushed on top when called, popped off when it returns |
| Removed | When the function returns or finishes |
Created in Two Phases
Like the global context, a function context is built in two steps.
Phase 1: Creation (memory creation).
- Stores the parameters with the values passed in.
- Creates the
argumentsobject (not for arrow functions). - Sets
this, based on how the function was called. - Allocates memory for local
varvariables (undefined) and inner functions (full code).
Phase 2: Execution. Runs the function body line by line and assigns real values.
var n = 2;function square(num) {var result = num * num;return result;}var square2 = square(n);
When square(n) is called, a new context is created. After its creation phase:
| Key | Value |
|---|---|
num | 2 (the argument passed in) |
result | undefined |
During its execution phase:
var result = num * num;replacesundefinedwith4.return result;sends4back to the calling context (the global one).- The function context is destroyed, and
square2becomes4.
A New Context for Every Call
Each call gets its own separate memory, so calls never mix up their values.
function greet(name) {var message = "Hi " + name;return message;}greet("Asha"); // context 1: name = "Asha"greet("Ravi"); // context 2: name = "Ravi" (a completely new one)
The Function Context and the Call Stack
1While square(n) runs:2 3| square() context | <- pushed on top (running now)4| Global context | <- bottom5 6After square(n) returns:7 8| Global context | <- square() context popped offIf a function calls another function, a new context is pushed on top of it, and the stack grows.
Summary
- A Function Execution Context is created every time a function is called.
- It holds parameters, the
argumentsobject, local variables, inner functions, andthis. - It is built in two phases: creation (memory allocation) and execution (line by line).
- Each call gets its own context, with its own separate memory.
- It is pushed onto the call stack when called and popped off when it returns.
Interview Answer: What Is a Function Execution Context?
A Function Execution Context is the execution context JavaScript creates every time a function is called. It has its own memory, which holds the function's parameters, the arguments object, local variables, and inner functions, and its own thread of execution.
It is created in two phases: in the creation phase, parameters get their values, local var variables are set to undefined, and inner functions are stored whole. In the execution phase, the body runs line by line. The context is pushed onto the call stack when the function is called and popped off when it returns, sending the return value back to the calling context.
The next topic is the call stack, which tracks all these contexts.