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
JavaScript Hoisting: Function Declarations vs Var Assignments
More Relevant Posts
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗔𝗿𝗿𝗮𝘆 𝗠𝗲𝘁𝗵𝗼𝗱𝘀 𝗘𝘃𝗲𝗿𝘆 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 𝗠𝘂𝘀𝘁 𝗞𝗻𝗼𝘄 JavaScript array methods help you write cleaner, shorter, and more readable code. This guide covers essential methods like map, filter, reduce, forEach, find, some, every, and sort, with real-world use cases and interview relevance. Perfect for frontend developers, JavaScript interviews, and daily revision. #JavaScript #ArrayMethods #JSBasics #FrontendDevelopment #WebDevelopment
To view or add a comment, sign in
-
Day 13/365 – Top #JavaScript Interview Questions 🔥Part2 Q19). What are higher-order functions? Q20). What is currying in JavaScript? Q21). What is an IIFE and why is it used? Q22). What is prototypal inheritance? Q23). What is debouncing and throttling? Q24). What is the difference between the spread operator and rest operator? Q25). What is the difference between Object.freeze() and Object.seal()? Q26). What is the difference between a Promise and an Observable? Q27). What is the difference between slice() and splice()? Q28). How do you optimize performance in JavaScript applications? Q29). What is the difference between synchronous and asynchronous code? Q30). What is the difference between null ,undefined and NaN? Q31). What are object methods like Object.keys(), Object.values(), and Object.entries()? Q32). What is the difference between DOM and BOM? Q33). What is destructuring in JavaScript and how is it useful? Q34). What is the difference between filter() and find()? Q35) How do you handle errors in JavaScript? Q36). What is Object.assign() and how does it work? #javascript #interview #webdevlopment #js #365daysofjs #jsinterview #interviewprepration
To view or add a comment, sign in
-
🔥 JavaScript Output-Based Question (Closures) ❓ Question What will be the output of the above code? 👉 Comment your answer below (Don’t run the code ❌) Output: 1 2 1 3 🧠 Why this output comes? (Step-by-Step Explanation) This example is all about JavaScript Closures. 1️⃣ Each function call creates a NEW closure const counter1 = createCounter(); const counter2 = createCounter(); counter1 and counter2 are created by separate executions Each execution creates its own count variable in memory So internally: counter1 → count = 0 counter2 → count = 0 (completely independent) 2️⃣ Execution Flow counter1() → count = 0 → 1 → prints 1 counter1() → count = 1 → 2 → prints 2 counter2() → different closure → count = 0 → 1 → prints 1 counter1() → back to first closure → count = 2 → 3 → prints 3 #JavaScript #Closures #FrontendDeveloper #MERNStack #ReactJS #InterviewQuestions
To view or add a comment, sign in
-
-
🧩 JavaScript Output-Based Question (call / bind) ❓ What will be logged? 👉 Comment your answer below (Don’t run the code ❌) Correct Output : A 🧠 Why this output comes? (Step-by-Step) 1️⃣ bind() creates a NEW function const bound = greet.bind(a); bind() permanently sets this to object a and returns a new bound function. 2️⃣ call() cannot change a bound this bound.call(b); Once a function is bound: • call() • apply() • bind() again ❌ cannot override the original binding So even though call(b) is used, this still points to a. That’s why : A is printed. 🔑 Key Takeaways ✔️ bind() sets this permanently ✔️ call() and apply() set this only temporarily ✔️ call() cannot override bind() ✔️ This is a very common interview trap If you remember one thing: bind beats call and apply #JavaScript #CallBindApply #InterviewQuestions #FrontendDeveloper #MERNStack #ReactJS
To view or add a comment, sign in
-
-
🤔 Ever wondered how JavaScript decides which variable a function can access? It’s not about where the function is called, it’s about where it’s written. 🧠 JavaScript interview question What is lexical scope in JavaScript? ✅ Short answer • Variable visibility is determined by where code is written • Inner scopes can access outer scopes • Outer scopes cannot access inner scopes 🔍 A bit more detail Lexical scope means: • Scope is fixed at definition (parse) time • Functions and blocks create scopes • Variable lookup walks outward through enclosing scopes JavaScript searches for variables: • In the current scope • Then parent scopes • Until it reaches the global scope 💻 Example const outer = "outside"; function test() { const inner = "inside"; console.log(outer); // works } test(); // console.log(inner); // ReferenceError ⚠️ Small but important detail Lexical scope is what makes closures possible. A function keeps access to the scope where it was defined, even after that outer function has returned. 🧩 Common misconceptions • Scope does NOT depend on who calls the function • Blocks with let and const create scope • Closures don’t create new scopes, they capture existing ones I’m sharing one JavaScript interview-style concept every day to build intuition, not just memorization. #javascript #frontend #webdevelopment #interviewprep
To view or add a comment, sign in
-
🛑 This JavaScript Output Looks Boring… But It Isn’t Most developers have never checked this property 👀 Yet it exists in every function. function sum(a, b, c = 10) {} console.log(sum.length); No logic. No return. No execution. Still… answers vary a LOT 😄 🤔 Why this question is interesting Tests function metadata Shows how JS treats default parameters Looks beginner-level Still surprises seniors Very common interview follow-up question 💬 Your Turn Comment your answer 👇 Output → Why? → ⚠️ Please don’t run the code. Think it through. I will post the correct output + simple explanation in the evening. 📌 This post is to understand JavaScript behavior, not production patterns. #JavaScript #LearnJS #FrontendDevelopment #CodingInterview #TechWithVeera #WebDevelopment
To view or add a comment, sign in
-
-
Your JavaScript code might be leaking memory… and you don’t even know it ⚠️ Closures are powerful, but they come with a hidden cost. When a closure captures its scope, the garbage collector can’t free anything in that scope, even variables the closure never touches. One tiny function can accidentally keep huge objects alive forever. That’s how silent memory leaks are born. If you want to write efficient JavaScript and sound confident in front end interviews, this is non-negotiable knowledge. 👉 Level up your memory management, and learn the patterns interviewers love. Check out GFE to master advanced JavaScript concepts that actually matter. https://lnkd.in/gY-cAPfx #JavaScript #FrontendDevelopment #WebDevelopment #ReactJS #greatfrontend
To view or add a comment, sign in
-
🚀 Day 15 | Mastering this, call(), apply(), and bind() in JavaScript Today, I learned one of the most important and commonly asked JavaScript concepts in interviews: this keyword and function methods call(), apply(), and bind(). 🔹 this keyword In JavaScript, this refers to the object that is currently calling the function. 🔹 call() Used to invoke a function immediately by setting the value of this and passing arguments normally. 🔹 apply() Same as call(), but arguments are passed as an array. 🔹 bind() Does not execute the function immediately. It returns a new function with a fixed this value, which can be called later. Very useful in DOM event handling to preserve context. 📌 Why is this important? These concepts help us control function context, avoid this related bugs, and write cleaner, more predictable JavaScript — especially while working with objects, events, and callbacks. 💡 Feeling more confident with JavaScript internals and one step closer to becoming a better developer! 🚀 #JavaScript #WebDevelopment #LearningJourney #Frontend #Programming #100DaysOfCode #Developer #DOM #InterviewPreparation
To view or add a comment, sign in
-
Day 15/50 – JavaScript Interview Question? Question: What is the Event Loop in JavaScript? Simple Answer: The Event Loop is the mechanism that handles asynchronous operations in JavaScript's single-threaded environment. It continuously checks the Call Stack and Task Queues, executing code in a specific order: synchronous code first, then microtasks (promises), then macrotasks (setTimeout, events). 🧠 Why it matters in real projects: Understanding the Event Loop is crucial for debugging asynchronous behavior, preventing UI blocking, and optimizing performance. It explains why promises execute before setTimeout even with 0ms delay. 💡 One common mistake: Not understanding the priority of microtasks vs macrotasks, leading to unexpected execution order in complex async code. 📌 Bonus: console.log('1: Start'); setTimeout(() => console.log('2: Timeout'), 0); Promise.resolve() .then(() => console.log('3: Promise 1')) .then(() => console.log('4: Promise 2')); console.log('5: End'); // Output order: // 1: Start // 5: End // 3: Promise 1 // 4: Promise 2 // 2: Timeout // Why? Sync code → Microtasks (Promises) → Macrotasks (setTimeout) #JavaScript #WebDevelopment #Frontend #LearnInPublic #InterviewQuestions #Programming #TechInterviews
To view or add a comment, sign in
-
𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁 𝗡𝗼𝘁𝗲𝘀 | 𝗙𝗿𝗼𝗺 𝗙𝘂𝗻𝗱𝗮𝗺𝗲𝗻𝘁𝗮𝗹𝘀 𝘁𝗼 𝗔𝗱𝘃𝗮𝗻𝗰𝗲𝗱 𝗖𝗼𝗻𝗰𝗲𝗽𝘁𝘀 JavaScript looks simple until interviews and real-world projects test your fundamentals. These JavaScript notes focus on the concepts that interviewers actually care about and that developers use daily. What these notes cover: • Execution Context & Call Stack • Hoisting (var / let / const) • Scope & Closures • this Keyword • Event Loop & Async JavaScript • Promises, Async/Await • Call, Apply & Bind • Prototypes & Inheritance • Currying & Higher-Order Functions • Debounce & Throttle • Shallow vs Deep Copy • Memory Management & Garbage Collection • ES6+ Features & Best Practices Useful for: Frontend & Full-Stack interviews Writing predictable, bug-free code Mastering JavaScript internals Tip: If you understand why JavaScript behaves a certain way, debugging becomes easy. #JavaScript #JS #FrontendDevelopment #WebDevelopment
To view or add a comment, sign in
Explore related topics
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