🔥 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
Output is : 1 2 1 3
1 2 1 3
1213
thanks for sharing