JavaScript Hoisting: Function Declarations vs Var Assignments

JavaScript Hoisting: A Tricky but Important Concept (Interview Favorite!) Output: 👉 World 🤔 What’s going on here? This happens because of JavaScript hoisting. Function declarations are hoisted completely (both name and body). Function expressions assigned to var are hoisted only as undefined. 🔍 Behind the scenes (Memory Creation Phase) JavaScript processes the code like this: function foo() { console.log("World"); } var foo; So when foo() is executed, JavaScript already knows about the function declaration — that’s why "World" is printed. 📌 Key Takeaways ✔ Function declarations have higher priority than var ✔ Avoid using the same name for functions and variables ✔ Understanding hoisting helps prevent unexpected bugs 💬 This is a very common JavaScript interview question, especially for frontend developers. #JavaScript #Hoisting #FrontendEngineering #WebDevelopment #JSConcepts #InterviewPreparation #CodingTips

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories