Memory Management And Hoisting in Javascript

Memory Management And Hoisting in Javascript

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 :

WhatsApp Image 2022-08-30 at 1.10.44 PM.jpeg

Phase 2 : Code Execution WhatsApp Image 2022-08-30 at 1.16.10 PM.jpeg when we run it

WhatsApp Image 2022-08-30 at 1.10.38 PM.jpeg

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

WhatsApp Image 2022-08-30 at 1.16.10 PM.jpeg

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