Execution Phase
Execution Phase
Section titled “Execution Phase”Introduction
Section titled “Introduction”The Execution Phase is where JavaScript actually runs the code line by line. Variables are assigned values, function calls are made, and new execution contexts are created.
What Happens
Section titled “What Happens”- Execute code line by line (top to bottom)
- Assign values to variables
- Execute function calls (creating new contexts)
- Handle operators, conditionals, loops
Example Walkthrough
Section titled “Example Walkthrough”var a = 10;let b = 20;const c = 30;
function greet(name) { return `Hello, ${name}!`;}
const message = greet('Alice');console.log(message);Step by Step Execution:
Section titled “Step by Step Execution:”var a = 10;→ assign 10 toa(wasundefined)let b = 20;→ assign 20 tob(was in TDZ)const c = 30;→ assign 30 tocfunction greet(name) {...}→ already stored in creation phaseconst message = greet('Alice');→ function call!- Create new function EC for
greet - Run its creation and execution phases
- Return value assigned to
message
- Create new function EC for
console.log(message);→ log the value
Function Call Flow
Section titled “Function Call Flow”flowchart TD A["Execution Phase: line 1"] B["Execution Phase: line 2"] C["Function Call Detected"] D["Pause current context"] E["Create & run function EC"] F["Return value"] G["Resume original context"]
A --> B --> C C --> D --> E --> F F --> G G --> H["Continue execution"]Assignment During Execution
Section titled “Assignment During Execution”// Creation phase: all vars are undefinedvar x; // x = undefined
// Execution phase: assignments happen herex = 10; // now x = 10x = 20; // now x = 20
// let/const are in TDZ until herelet y; // y is in TDZ during creationy = 30; // TDZ ends, y = 30Summary
Section titled “Summary”- Code runs line by line during execution phase
- Variable assignments happen here (not creation)
- Function calls create new contexts (pausing current one)
- After function returns, original context resumes
- The execution phase follows the creation phase