🚀 JavaScript Hoisting: A Small Interview Question That Confuses Many ✅ Today I came across an interesting JavaScript interview question : Why does console.log(bar) print the function instead of undefined? I was asked an interesting JavaScript question in an interview today, and it was a great reminder of how differently JavaScript handles hoisting for function expressions vs. function declarations. Here’s the concept behind the output: 1. **boo** is created using a function expression with **var**. Variables declared with var are hoisted, but only the variable name is initialized. The actual function value is not assigned until later. So when boo is logged before assignment, it returns undefined. 2. **bar** is created using a function declaration. Function declarations are fully hoisted. This means the entire function is placed into memory before any execution happens. So when **bar** is logged, JavaScript prints the complete function definition. 3. After execution reaches their definitions: Both **boo()** and **bar()** run normally because they now hold their respective function values. Key takeaway: **var** hoists only the variable, not the function value. Function declarations hoist both the name and the entire function body. A simple interview question, but a powerful reminder of how JavaScript behaves behind the scenes. #JavaScript #WebDevelopment #FrontendDeveloper #CodingInterview #TechInterview #JavaScriptTips #Hoisting #LearnToCode #Developers #Programming #MERNStack #ReactJS #NodeJS #100DaysOfCode
JavaScript Hoisting: var vs Function Declarations
More Relevant Posts
-
🚀 JavaScript Interview Question That Many Developers Get Wrong! I recently came across an interesting JavaScript interview question: 💡 What will be the output? ➡️ 10 🔍 Why? Because JavaScript has: Function scope Local variable shadowing Closures Even though x = 15 creates a global variable, the inner() function has its own: let x = 10; This local variable shadows the global one. So the global x never affects the value returned by inner(). Understanding these small details is what separates a good JS developer from a great one! 💪 🧠 Key Takeaways ✔ JavaScript is single-threaded but supports async behavior ✔ Local variables always override global variables ✔ Closures retain access to their lexical scope ✔ Missing let/const creates global variables (bad practice!) #javascript #webdevelopment #interview #coding #frontend #reactjs #mern #nextjs #learnjavascript #softwareengineering
To view or add a comment, sign in
-
-
🔥 Different Ways to Write Functions in #JavaScript In JavaScript, functions are first-class citizens — and there’s more than one way to write them. Here are the most commonly used function types every JS developer should know 👇 ✅ Function Declaration ✅ Function Expression ✅ Arrow Function ✅ Anonymous Function ✅ IIFE (Immediately Invoked Function Expression) ✅ Object Method 💡 Why this matters? Understanding how and when to use each type helps in: Writing clean & readable code Handling callbacks and async logic Cracking JavaScript interviews Understanding frameworks like React internally 📌 Tip: Interviews don’t just check if you know functions — they check why you chose that syntax. If you’re learning JavaScript or preparing for interviews, save this post 🔖 More JS concepts coming soon 🚀 Follow Rahul Choudhary for more. JavaScript Mastery w3schools.com #JavaScript #FrontendDevelopment #WebDevelopment #JSFunctions #LearnJavaScript #CodingInterviews #ReactJS #Developers
To view or add a comment, sign in
-
-
🔥 Most Repeated JavaScript Interview Questions 1. What is Hoisting in JavaScript? 2. Difference between var, let, and const. 3. What is the Event Loop? 4. What is the difference between synchronous and asynchronous code? 5. What are Promises and how do they work? 6. Difference between Promise.all, Promise.race, Promise.allSettled, and Promise.any. 7. What is async/await and how does it improve async code? 8. What are Closures? Explain with an example. 9. What is the Temporal Dead Zone (TDZ)? 10. How does the this keyword work in different contexts? 11. Difference between call, bind, and apply. 12. What is Event Bubbling and Capturing? 13. What is Event Delegation? 14. How does prototypal inheritance work? 15. How does the new keyword work internally? 16. What is the difference between == and === ? 17. What are Higher-Order Functions? 18. What is Debouncing? 19. What is Throttling? 20. What is the difference between map, filter, and reduce? 21. What is a shallow copy vs deep copy? 22. What is the spread operator and rest operator? 23. What is destructuring in JavaScript? 24. What are arrow functions and how do they differ from normal functions? 25. What is a callback function? 26. What is an IIFE (Immediately Invoked Function Expression)? 27. What is a module in JavaScript (ES modules vs CommonJS)? 28. What are microtasks vs macrotasks? 29. What is Optional Chaining (?.)? 30. What is Nullish Coalescing (??)? #JavaScript #JavaScriptInterview #FrontendDeveloper #WebDevelopment #ReactJS #CodingInterview #InterviewPreparation #FrontendInterview
To view or add a comment, sign in
-
🔥 Different Ways to Write Functions in JavaScript In JavaScript, functions are first-class citizens — and there’s more than one way to write them. Here are the most commonly used function types every JS developer should know 👇 ✅ Function Declaration ✅ Function Expression ✅ Arrow Function ✅ Anonymous Function ✅ IIFE (Immediately Invoked Function Expression) ✅ Object Method 💡 Why this matters? Understanding how and when to use each type helps in: Writing clean & readable code Handling callbacks and async logic Cracking JavaScript interviews Understanding frameworks like React internally 📌 Tip: Interviews don’t just check if you know functions — they check why you chose that syntax. If you’re learning JavaScript or preparing for interviews, save this post 🔖 More JS concepts coming soon 🚀 #JavaScript #FrontendDevelopment #WebDevelopment #JSFunctions #LearnJavaScript #CodingInterviews #ReactJS #Developers
To view or add a comment, sign in
-
-
🔥 Different Ways to Write Functions in JavaScript In JavaScript, functions are first-class citizens — and there’s more than one way to write them. Here are the most commonly used function types every JS developer should know 👇 ✅ Function Declaration ✅ Function Expression ✅ Arrow Function ✅ Anonymous Function ✅ IIFE (Immediately Invoked Function Expression) ✅ Object Method 💡 Why this matters? Understanding how and when to use each type helps in: Writing clean & readable code Handling callbacks and async logic Cracking JavaScript interviews Understanding frameworks like React internally 📌 Tip: Interviews don’t just check if you know functions — they check why you chose that syntax. If you’re learning JavaScript or preparing for interviews, save this post 🔖 More JS concepts coming soon 🚀 #JavaScript #FrontendDevelopment #WebDevelopment #JSFunctions #LearnJavaScript #CodingInterviews #ReactJS #Developers
To view or add a comment, sign in
-
-
🔹 JavaScript Prototype & Prototypal Inheritance (Simple Explanation) This concept was asked in my recent interview, so sharing a clear and simple explanation. ✅ What is Prototype? In JavaScript, every object has a hidden link called [[Prototype]]. 👉 If a property or method is not found on the object, JavaScript looks for it in its prototype. This helps share methods without duplicating them. ✅ Prototype on Functions Every function has a .prototype property. We use it to add methods that all created objects can share. ✅ What is Prototypal Inheritance? When an object uses a method from its prototype, it is called prototypal inheritance. 🎯 Simple Summary Prototype → a hidden link that connects objects Prototypal inheritance → object accessing parent’s methods through prototype chain JavaScript searches: object → prototype → Object.prototype → null Understanding this makes JavaScript and React internals much clearer 🚀 #JavaScript #Frontend #Interviews #WebDevelopment #Learning
To view or add a comment, sign in
-
-
🚀 Top 100 JavaScript Interview Questions (Must-Know for 2025) JavaScript interviews are becoming tougher — not because the questions are new, but because companies now expect deeper clarity, clean explanations, and hands-on understanding. That’s why I compiled a list of 100 essential JavaScript interview questions that every frontend developer (beginner → senior) should master before walking into an interview. ✅ Here’s a quick preview of the categories covered: 1️⃣ Core JavaScript Concepts Hoisting Scope (var, let, const) Execution Context Event Loop Closures Prototypal Inheritance 2️⃣ Modern JavaScript (ES6+) Arrow functions Spread / Rest Destructuring Promises Async/Await Modules 3️⃣ Advanced Concepts Debouncing & Throttling Memoization Currying Deep vs Shallow Copy Call, Bind, Apply Higher-Order Functions 4️⃣ Browser & DOM Event Bubbling & Capturing Web APIs LocalStorage vs SessionStorage Fetch API CORS 5️⃣ Coding Challenges Reverse string without built-ins Implement map/filter polyfills Flatten nested array Remove duplicates Anagram check ✅ Top 100 JavaScript Interview Questions (2025 Ready) follow the link of list of questions https://lnkd.in/d9cEGC_b #javascript #frontendinterview #codinginterview #frontenddeveloper #techcareers #javascriptdeveloper
To view or add a comment, sign in
-
-
#javascript Array.map() feels obvious when you use it. It doesn’t feel obvious when you try to write it. While implementing it from scratch, a few things became very clear: • map() always creates a new array Touching the original one means it’s wrong. • You can’t assume all indexes exist Sparse arrays force you to check before reading values. • The loop is not the important part The callback is what shapes the result. • thisArg affects real behavior Ignoring it breaks how map() is expected to work. • The callback gets more than just the value Index and array access enable many real-world use cases. The implementation is small. The thinking behind it isn’t. This is exactly why Array.map() is asked in JavaScript interviews. 📺 I’ve broken this down step-by-step in my JavaScript Interview Series on YouTube Link is in the first comment If you’ve never written map() yourself, it’s worth doing once. #JavaScript #JavaScriptInterview #FrontendDevelopment #ArrayMethods
To view or add a comment, sign in
-
🧠 JavaScript Interview Question – Test Your Fundamentals What will be the output of the following JavaScript code, and why? function A() {} A.prototype = { foo: function() { return 1; } }; const a = new A(); A.prototype.foo = function( ) { return 2; }; console.log(a.foo( )); 💬 Drop your answers in the comments with explanations. Bonus points if you explain it like you would in a real interview 😉 #JavaScript #Frontend #WebDevelopment #InterviewQuestions #Learning #Developers
To view or add a comment, sign in
-
Interview Question: What is a Closure in JavaScript? - A closure is created when a function remembers and accesses variables from its outer (lexical) scope, even after the outer function has finished executing. In simple words: A function + its outer scope = Closure ● Example of Closure : function outer() { let count = 0; function inner() { count++; console.log(count); } return inner; } const fn = outer(); fn(); // 1 fn(); // 2 -> inner() remembers count even after outer() is done. ● Why Closures are Useful? - Data hiding / encapsulation - Maintaining state - Callback functions - Event handlers • Interview Tip: Closures are possible because JavaScript uses lexical scoping, not dynamic scoping. #JavaScript #InterviewPrep #Closures #WebDevelopment #Frontend #MERN #LearnInPublic #CodingJourney #Backend #30DaysOfJavaScript #Backend #BDRM #BackendDevWithRahulMaheshwari
To view or add a comment, sign in
-
More from this author
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
https://www.garudax.id/posts/ali-hamza-5b501030a_understanding-hoisting-in-javascript-activity-7402856891346845696-IIhl?utm_source=share&utm_medium=member_desktop&rcm=ACoAAE6q9FkBzCo74BuVHNhBG-2nfhiIoHyxd20