Memory Management in Javascript
How is Memory allocation done in Javascript? A javascript program Memory allocation has two phases
Phase 1 A global execution context is created and all the variables and functions are assigned memory.This process happens even before we have clicked the run button to run it.The global execution space is divided into two parts 1.Memory 2.Code
Example:
For the code snippet above the global context block will look like the following (image below) in the first phase :
Phase 2 : Code Execution
when we run it
The use of call Stack in Javascript : When run the javascript program at first the global execution place is put After that all the function call made in the program are inserted
Example For the code snipped mentioned above the call stack will look like
Hoisitng in Javascript
let vs const vs var
All of the 3 mentioned above are hoisted in javascript, But when we try to access let and const before initialization we get a reference error. The reason behind this is that let and const are present in the temporal dead zone Temporal Dead Zone: it is a zone where the variables can be accessed only when they are initialized Hence it appears as if let and const are not hoisted but underlying they are hoisted but are present in the temporal dead zone