- Memory Creation Phase
- Code Execution Phase
Memory Creation Phase
In the Memory Creation Phase, all the variables and functions are stored as key-value pairs. The Memory Creation Phase is also known as the Variable Environment.
In the Memory creation phase, the memory will be allocated to all variables and functions inside the Global Execution Context. In this phase, variable declarations are scanned and made
- It stores undefined to all variables.
- For Functions, it stores the entire function code inside memory space.
Code Execution Phase
Code Execution Phase is the phase when code is executed one line at a time. The Code Execution Phase is also known as the Thread of Execution.
After completion of function execution, the result is stored inside the Memory Component. Once the execution is done and the result is stored inside the Memory Component the local execution context is deleted from the code component.
1. Memory Creation Phase (Variable Environment):
- Variable declarations are scanned, and they are set to
- Functions are stored in memory with their entire code.
2. Code Execution Phase (Thread of Execution):
- This is where the actual code is executed line by line. Calculations are performed, and values are assigned to variables.
- When a function is called, a new local execution context is created with its own memory and two phases, similar to the global context.
- After a function finishes executing, its result is stored in memory, and the local execution context is removed.
Let’s break it down in a simpler way:
1. Memory Creation Phase (Variable Setup):
- It sets up space in memory for things like variables and functions.
- Variables are prepared but don’t have values yet; they are like empty boxes.
- Functions are stored in memory with all their instructions, like a recipe.
2. Code Execution Phase (Doing the Work):
- It works one step at a time, following the order of your code.
- When it sees a variable, it gives it a value.
- If there’s a function, it runs that function like a small program.
- After the function finishes, the result is saved, and the function’s stuff is removed.
Think of it like making a list of what you need to do (Memory Creation Phase) and then actually doing each task one by one (Code Execution Phase). When you have a recipe (function), you follow the steps in the recipe, and when you’re done, you put away the recipe and keep the dish you made.
Jai Hind, Vande Mataram 🇮🇳
- What is React
- Pass by Value & Pass by Reference — Simplified