
How JavaScript Code is executed? ❤️& Call Stack | Namaste JavaScript Ep. 2
Akshay Saini
Overview
This video explains the fundamental process of how JavaScript code is executed within the JavaScript engine. It details the creation and lifecycle of Execution Contexts, which are essential for running any JavaScript code. The explanation covers the two main phases of Execution Context creation: the Memory Creation Phase (where variables and functions are allocated memory) and the Code Execution Phase (where the code is actually run and values are assigned). It further elaborates on how function invocations lead to the creation of new, nested Execution Contexts and how the Call Stack is used to manage these contexts, ensuring the correct order of execution and deletion.
Save this permanently with flashcards, quizzes, and AI chat
Chapters
- Every JavaScript program runs within an Execution Context.
- An Execution Context is like a container that holds information about the environment in which JavaScript code is evaluated.
- It is crucial for understanding how JavaScript code behaves.
- The global scope of a JavaScript program has a Global Execution Context.
- Execution Contexts are created in two distinct phases: Memory Creation (or Creation Phase) and Code Execution Phase.
- During the Memory Creation Phase, JavaScript scans the code and allocates memory for all variables and functions. Variables are assigned a placeholder value of `undefined`, while functions have their entire code stored.
- During the Code Execution Phase, JavaScript runs the code line by line, assigning actual values to variables and executing functions.
- When a function is invoked, a new, separate Execution Context is created specifically for that function call.
- This new context also goes through the same two phases: Memory Creation and Code Execution.
- Parameters passed to a function are handled during the Memory Creation Phase of the new context, and arguments are passed from the calling context.
- Upon function completion, its Execution Context is deleted, and control returns to the calling context.
- JavaScript uses a Call Stack to manage the order of Execution Contexts.
- The Global Execution Context is at the bottom of the stack when the program starts.
- Each time a function is invoked, its Execution Context is pushed onto the top of the stack.
- When a function finishes executing, its context is popped off the stack, and control returns to the context below it.
- The Call Stack ensures that functions are executed and completed in the correct order.
- The Call Stack is known by several other names, including Execution Context Stack, Program Stack, Control Stack, and Runtime Stack.
- Once all functions have finished executing and their contexts have been popped off the stack, the Call Stack becomes empty.
- The JavaScript program then terminates.
Key takeaways
- JavaScript code execution is managed by Execution Contexts, which are created for global code and each function call.
- The two-phase creation process (Memory Creation and Code Execution) is key to understanding variable initialization and code running.
- Function invocations create new, nested Execution Contexts, each with its own memory and code execution scope.
- The Call Stack is the mechanism JavaScript uses to keep track of and manage the order of these nested Execution Contexts.
- When a function completes, its Execution Context is removed from the Call Stack, returning control to the previous context.
- Understanding the Call Stack helps in debugging and comprehending JavaScript's execution flow, especially with complex or asynchronous code.
Key terms
Test your understanding
- What are the two main phases involved in the creation of a JavaScript Execution Context, and what happens in each phase?
- How does JavaScript manage the creation and deletion of Execution Contexts when functions are called within other functions?
- What is the primary role of the Call Stack in JavaScript execution?
- Why is it important to understand the concept of `undefined` during the Memory Creation Phase of an Execution Context?
- How does the Call Stack ensure that code is executed in the correct order, especially when multiple functions are involved?