Full Stack / 3 min read
I Asked 30 Developers This Question — Only 1 Got It Right -Part2
It’s not hard. It’s just tricky in the way only JavaScript can be.
Part 2: I Asked 30 Developers This Question — Only 1 Got It Right
It’s not hard. It’s just tricky in the way only JavaScript can be.

A little Game with my Readers
Let’s play a little game.
Imagine this is your frontend round.
You’re 25 minutes into the interview. It’s going well.
And then the interviewer drops this:
function outer() {
var counter = 0;
return function inner() {
return ++counter;
};
}const countA = outer();
const countB = outer();
console.log(countA()); // ?
console.log(countA()); // ?
console.log(countB()); // ?Take 10 seconds. Really think through it.
No cheating. No ChatGPT.
Just you and your brain.
Let me drop you a hint, as I am your friend. It’s closure concept
Most Common Answer (And Why It’s Wrong)
Most devs said:
1
2
3Which feels right at first glance. But it’s wrong.
Why?
Because they’re assuming the two functions (countA, countB) are sharing the same counter. They’re not.
The Real Output
1
2
1Here’s why:
- Every call to
outer()returns a new closure. - So
countAandcountBeach get their own separatecounter.
That’s the magic of closures.
Each inner() keeps access to its own counter, even after outer() has finished executing.
Why This Question Works
This isn’t about memorising syntax.
It’s about understanding:
- Scope
- Closures
- Function factories
- And how JavaScript preserves state between function calls
I know it’s confusing, it was for me too initially. You can read more about closures here
Want to Go Deeper? Here’s a Challenge
What will this log?
const arr = [];for (var i = 0; i < 3; i++) {
arr.push(function () {
console.log(i);
});
}
arr[0]();
arr[1]();
arr[2]();Now try the same with let instead of var. 👀
Why This Matters in Real Life
Closure power:
- Private variables
- Memoization functions
- Event listeners that persist internal state
- React hooks (yes, even
useStaterelies on closure behaviour)
If you don’t understand closures, you’ll hit invisible walls when building even moderately complex apps.
Closure is one of the important concept in interviews even while building complex objects.
Key Takeaways
- Every time you call a function that returns another function, you’re creating a closure.
- Closures help retain scope across function calls.
- Don’t just memorize answers — try tweaking the code to really understand what’s happening.
Your Turn
What JS topic do you secretly struggle with?
Drop it in the comments — maybe I’ll turn it into Part 3. 😉
Or tag a dev who’d love this question.
Let’s turn tricky JS into fun challenges — together.
If you want to prepare with us to ace your next interview. You can book a call here
At Dev Simplified, We Value Your Feedback 📊
👉 Follow us to not miss any updates.
👉 Have any suggestions? Let us know in the comments!