Creation Phase
Creation Phase
Section titled “Creation Phase”Introduction
Section titled “Introduction”The Creation Phase (also called Memory Allocation Phase) is the first of two phases in every execution context. During this phase, JavaScript sets up memory space for variables and functions before any code is executed.
What Happens
Section titled “What Happens”- Create the LexicalEnvironment (scope)
- Create the VariableEnvironment
- Set up the
thisbinding - Allocate memory for variables (initialized to
undefined) - Store function declarations (fully hoisted)
Example
Section titled “Example”// What you write:console.log(myVar); // undefinedvar myVar = 10;
// Behind the scenes (creation phase):// var myVar; → allocated, initialized to undefined// Then execution phase runs:// console.log(myVar); → undefined// myVar = 10; → assignedHoisting During Creation
Section titled “Hoisting During Creation”// During creation phase:var a; // allocated, set to undefinedfunction greet() {} // fully stored in memorylet b; // allocated, NOT initialized (TDZ)const c; // allocated, NOT initialized (TDZ)The this Binding
Section titled “The this Binding”// Global context: this = global objectconsole.log(this); // window (browser)
// Function context: this depends on how the function is calledfunction showThis() { console.log(this);}Summary
Section titled “Summary”- Creation phase runs before any code executes
- Variables with
varare initialized toundefined - Function declarations are fully hoisted
letandconstare allocated but not initializedthisbinding is set up