Hoisting and Execution Context
What is hoisting in JavaScript?
Hoisting is a JavaScript behavior where variable and function declarations are conceptually moved to the top of their containing scope during the compilation phase, before code execution. This means that regardless of where variables and functions are declared within a scope, they are made available throughout that scope. However, only the declarations are hoisted, not the initializations. For example, with variables declared using `var`, the declaration is hoisted but the initialization remains in place. Function declarations (not expressions) are hoisted entirely with their implementation. This behavior is part of JavaScript's two-phase processing: first, the creation phase where declarations are processed and hoisted, and second, the execution phase where the code is run line by line. Understanding hoisting is crucial for avoiding unexpected behavior in JavaScript code.