⚡ What Is Hoisting in JavaScript?
⚡ What Is Hoisting in JavaScript? Hoisting means that JavaScript moves variable and function declarations to the top of their scope (before code execution) — but not the initialization . So JavaScript “knows” about your variables and functions before it runs your code, but values are not yet assigned. ๐ง Example 1 — Variable Hoisting (with var ) console.log(a); // ๐ undefined var a = 10; ๐ Behind the Scenes JavaScript internally does this: var a; // Declaration is hoisted console.log(a); a = 10; // Initialization stays in place ✅ So it doesn’t throw an error — it just logs undefined . ๐ซ let and const Are Not Fully Hoisted console.log(b); // ❌ ReferenceError: Cannot access 'b' before initialization let b = 20; ✅ Explanation: let and const are hoisted , but they are not initialized until the execution reaches that line. The time between hoisting and initialization is called the Temporal Dead Zone (TDZ) . ๐งฉ Example 2 — Function Hoi...