i.e., the code that is not inside a function
this
this
inside the functionExecution contexts have following two phases:
During this phase, the variable declarations and references to functions are saved as key-value pairs inside the execution context.
The value of this
and a reference to the outer environment are also set during this phase
values for variables are not assigned during the creation phase.
variables that refer to functions do refer to functions during this phase
Variables declared using var
are assigned undefined
as their value during this phase, while variables declared using let
or constants declared using const
are left uninitialized.
In the case of a global context, there is no outer environment, so reference to the outer environment is set to null, but in the case of a function context, the value of this
depends on how the function is called, so the value of this
is set appropriately
function
declarationslet
const
let name = "Jane Doe";
var age = 20;
function introduce(name, age) {
console.log("Hello, I am " + name + " and I am " + age + " years old");
}
different variables in the execution context are yet to be assigned their respective values
Assignments are done during the execution phase, and the code is finally executed
A call stack is a structure that is used internally by the JavaScript engine to keep track of the piece of code that is currently executing.
simply a stackĀ²Ā² data structure that aids in the execution of the JavaScript code by keeping track of currently executing code
a collection of execution contexts
Before executing any JavaScript code, a global execution context is created and pushed on the call stack.
label āglobalā is not important Google Chrome browser shows ā(anonymous)ā instead of āglobal.ā
After pushing the global execution context on the call stack, any function calls encountered during the execution of the code will lead to more entries in the call stack.
For every function call, a new entry is added to the call stack before that function starts executing, and as soon as the function execution ends, that entry is popped off the stack.
top element in the call stack represents the currently executing piece of code
call stack has a fixed size and can contain a limited number of entries.
This error is thrown when the call stack gets filled up to its limit and can no longer hold more entries.
JavaScript engines may allocate most values on the heap but could use the stack for optimization and store temporary values that might not last longer than a function call.
different JavaScript engines may handle memory differently, and āprimitives in javaScript simply go on the stackā is a misconception.
Memory that is no longer needed is automatically freed by the JavaScript engine
garbage collection
Currently, modern JavaScript engines use a Mark-and-sweep algorithmĀ²āø.
This algorithm determines which blocks of memory are āunreachableā
This algorithm is an improvement over the reference counting algorithmĀ²ā¹, which has its limitations.