Hoisting is generally discussed as moving of variables and function to the top of code before execution. But, actually its moving functions and variables to memory first (During compile phase) while there physical position remains the same in code.
Uses :
You can access functions before you define them in the workflow.
hello();
functionhello(){ console.log("Hello");}
This will work even while calling hello(), the function is not defined physically.
Similarly ,
You can access variables before you declare them .
num = 6;
var num;
This will also work as num declaration is moved to memory first before execution starts.
PS : Only declarations are hoisted not their initialization.
Atul Sharma
Full Stack Developer | Cloud Native Applications
Hoisting is generally discussed as moving of variables and function to the top of code before execution. But, actually its moving functions and variables to memory first (During compile phase) while there physical position remains the same in code.
Uses :
You can access functions before you define them in the workflow.
hello(); function hello(){ console.log("Hello");}This will work even while calling hello(), the function is not defined physically.
Similarly ,
You can access variables before you declare them .
num = 6; var num;This will also work as
numdeclaration is moved to memory first before execution starts.PS : Only declarations are hoisted not their initialization.