๐ ๐๐ง๐๐๐ซ๐ฌ๐ญ๐๐ง๐๐ข๐ง๐ ๐๐๐ฏ๐๐๐๐ซ๐ข๐ฉ๐ญ ๐๐ฑ๐๐๐ฎ๐ญ๐ข๐จ๐ง ๐๐จ๐ง๐ญ๐๐ฑ๐ญ ๐
๐ ๐พ๐๐๐ ๐๐ ๐๐ ๐ฌ๐๐๐๐๐๐๐๐ ๐ช๐๐๐๐๐๐?
When JavaScript code starts running, it creates a special environment called the ๐๐ญ๐ฐ๐ฃ๐ข๐ญ ๐๐น๐ฆ๐ค๐ถ๐ต๐ช๐ฐ๐ฏ ๐๐ฐ๐ฏ๐ต๐ฆ๐น๐ต (๐๐๐). Think of it as a container that holds everything related to your script.
๐น ๐๐ฐ๐ฎ๐ฑ๐ฐ๐ฏ๐ฆ๐ฏ๐ต๐ด ๐ฐ๐ง ๐ข๐ฏ ๐๐น๐ฆ๐ค๐ถ๐ต๐ช๐ฐ๐ฏ ๐๐ฐ๐ฏ๐ต๐ฆ๐น๐ต
A GEC has two main parts:
- ๐ง ๐๐ฆ๐ฎ๐ฐ๐ณ๐บ: Stores variables and functions.
- ๐ ๐๐ฐ๐ฅ๐ฆ: Contains the actual JavaScript code.
๐ Creation of an Execution Context
The GEC is created in two phases:
- ๐ ๏ธ ๐ช๐๐๐๐๐๐๐ ๐ท๐๐๐๐:
๐ฆ ๐๐ฆ๐ฎ๐ฐ๐ณ๐บ ๐๐ญ๐ญ๐ฐ๐ค๐ข๐ต๐ช๐ฐ๐ฏ: All variables are allocated memory and initialized with ๐ถ๐ฏ๐ฅ๐ฆ๐ง๐ช๐ฏ๐ฆ๐ฅ.
๐ ๐๐ถ๐ฏ๐ค๐ต๐ช๐ฐ๐ฏ ๐๐ฆ๐ค๐ญ๐ข๐ณ๐ข๐ต๐ช๐ฐ๐ฏ๐ด: Functions are stored in memory as complete code blocks. This is known as ๐ฉ๐ฐ๐ช๐ด๐ต๐ช๐ฏ๐จ.
- โ๏ธ ๐ฌ๐๐๐๐๐๐๐๐ ๐ท๐๐๐๐:
โถ๏ธ Code Execution: The code is executed line by line.
๐ Variable Assignment: Variables are assigned their actual values.
๐ Function Invocation: When a function is called, a new execution context is created for that function and pushed onto the ๐ค๐ข๐ญ๐ญ ๐ด๐ต๐ข๐ค๐ฌ.
๐ Return: Once the function finishes, its context is popped off the stack, and execution returns to the previous context.
๐ ๐บ๐๐๐๐ ๐๐๐ ๐ฏ๐๐๐๐๐๐๐
๐ ๐บ๐๐๐๐ : Determines the accessibility of variables. Variables declared within a function have function scope, while those declared outside have global scope.
๐ผ ๐ฏ๐๐๐๐๐๐๐: Function declarations and variable declarations are moved to the top of their scope during the creation phase, but variable assignments remain in their original positions.