How JavaScript Hoisting Works: Functions, Var, Let, Const

🚀 Understanding Hoisting in JavaScript Ever wondered how you can use a variable or function before it’s declared? 🤔 That’s because of Hoisting! In JavaScript, hoisting means that variable and function declarations are moved to the top of their scope during the compile phase — before the code actually runs. 🧠 Example: greet(); // ✅ Works! function greet() { console.log("Hello, World!"); } Here, the function greet() is hoisted to the top — that’s why you can call it even before it’s defined. But be careful with variables 👇 console.log(x); // ❌ undefined var x = 10; Variables declared with var are hoisted but not initialized, so they exist but hold undefined. However, variables declared with let and const are also hoisted but stay in the Temporal Dead Zone (TDZ) until they’re actually declared. 📌 In short: Functions → hoisted with their definitions ✅ var → hoisted but undefined ⚠️ let & const → hoisted but inaccessible until declared 🚫 #javascript #frontend #webdevelopment

  • graphical user interface, application

To view or add a comment, sign in

Explore content categories