Hoisting in JavaScript
Hoisting is a phenomenon in JavaScript, in which we can access variables and functions without any error before their initialization. This happens because even before the code starts executing, memory is allocated to variables and functions. JavaScript is synchronous and single threaded language where everything happens inside a Global Execution Context. When we run a JS program, an execution context is created, which is further divided into two phases: Memory Allocation Phase (Variable Environment) Code Execution Phase (Thread of Execution) In the first phase, the entire program is skimmed line by line and memory is allocated to functions and variables. In case of variables, undefined is stored as a placeholder and in case of functions, the whole code of function is stored. In the second phase, the code is actually executed line by line. Undefined keyword is now replaced by actual values of variables. When there is a function invocation, a entirely new execution context is created whi...