Home › Interview Questions › What is the output of the following JavaScript cod…

What is the output of the following JavaScript code? ```js function outer() { let count = 0; return function inner() { count++; console.log(count); }; } const fn = outer(); fn(); fn(); fn(); ```

🟢 Easy Coding Fresher level
1Times asked
May 2026Last seen
May 2026First seen

💡 Model Answer

The function outer returns a closure that captures the variable count. When outer is invoked, count is initialized to 0 and the inner function is returned. Each call to fn() executes the inner function, which increments count and logs its value.

The sequence of console outputs is:

1

2

3

After the first call, count becomes 1 and is logged. The second call increments it to 2, and the third call increments it to 3. The closure preserves the state of count across calls, demonstrating how closures can maintain private state in JavaScript.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500