Pir Gee

Think You're a JavaScript Pro? Try Solving This Puzzle!

ByZeenat Yasin

6 November 2025

Think You're a JavaScript Pro? Try Solving This Puzzle!

* All product/brand names, logos, and trademarks are property of their respective owners.

Think you’ve mastered JavaScript? Maybe you've been coding for years, debugging dozens of apps, and building everything from landing pages to dynamic SPAs. But here’s the real test — can you really say you know JavaScript until you’ve tackled a puzzle that exposes the language’s quirks, hidden behaviors, and logic traps?

In the world of programming, JavaScript stands out not just for its flexibility, but also for its weirdness. It’s the kind of language that lets you write [] + [] and get "", or return true == '1' — and for most developers, this strangeness is both a gift and a curse.

That’s where JavaScript puzzles come in. These little brain-teasers aren’t just for fun (though they are fun). They’re powerful learning tools — helping you uncover the deeper rules of the language, sharpen your debugging instincts, and prep for high-level interviews or real-world production issues.

In this blog, we’re going to put your skills to the test with a tricky JavaScript puzzle that’s stumped more than a few developers. It’s not about memorizing syntax — it’s about understanding the "why" behind how JavaScript runs your code.

Spoiler alert: many experienced developers get this one wrong on the first try.

Are you ready to prove you’re a JavaScript pro?
Let’s dive in.

The Puzzle — Can You Solve It?

The Setup

Let’s imagine you’re debugging a real-world JavaScript issue. You come across a function that’s supposed to loop and store functions in an array — each function logging the index from the loop. Simple, right?

Here’s the code:

function createLoggers() {
  var loggers = [];

  for (var i = 0; i < 3; i++) {
    loggers.push(function () {
      console.log(i);
    });
  }

  return loggers;
}

const myLoggers = createLoggers();

myLoggers[0](); // ?
myLoggers[1](); // ?
myLoggers[2](); // ?

What do you think the output will be?
Take 30 seconds. Think about how JavaScript handles var, closures, and loops.

What’s Your Answer?

Here are some common guesses:

  • 0, 1, 2 — seems intuitive, right?

  • undefined, undefined, undefined — maybe it's an error?

  • 3, 3, 3 — wait, what?

Drop your guess in the comments — no cheating!

This kind of question is exactly the kind of logic trap that shows up in interviews, code reviews, and debugging sessions. The syntax is simple, but the behavior? Sneaky.

Common Developer Reactions

Many developers assume each function "remembers" its own i value at the time it was created. But with var, that’s not how JavaScript works.

We’ve seen even senior engineers confidently predict 0, 1, 2 — only to be shocked when every log outputs 3.

Sound familiar?

Don’t worry — in the next section, we’ll break down exactly what’s going on, and why this puzzle trips up even the best.

Solution Breakdown & What It Teaches You

Step-by-Step Explanation

Let’s go back to this part of the puzzle:

for (var i = 0; i < 3; i++) {
  loggers.push(function () {
    console.log(i);
  });
}

At first glance, it looks like we’re storing three separate functions, each remembering a different value of i. But here’s the catch: the keyword var does not create a new i for each loop iteration. Instead, var is function-scoped, not block-scoped.

That means:

  • The same i variable is shared across all iterations.

  • The functions you push into loggers[] don’t get the value of i at that moment.

  • They just reference the i variable, and when they’re finally executed (after the loop ends), i has already become 3.

So when you run:

myLoggers[0](); // logs 3
myLoggers[1](); // logs 3
myLoggers[2](); // logs 3

Output:

3  
3  
3  

The JavaScript Concepts Behind It

This puzzle teaches you three powerful concepts:

1. Closures

Every function in JavaScript "closes over" the variables in its outer scope — but it doesn’t make a copy of them. It keeps a reference. That’s why all three functions point to the same i.

2. Function vs Block Scope

  • var is function-scoped. That means no matter how many times the loop runs, it reuses the same i.

  • If we changed var to let, each iteration would create a new block-scoped i.

Try this instead:

for (let i = 0; i < 3; i++) {
  loggers.push(function () {
    console.log(i);
  });
}

Now the output is:

0  
1  
2  

Magic? No — just modern JavaScript behaving as expected.

3. Deferred Execution

Your functions are stored and executed after the loop completes. So by the time myLoggers[0]() runs, i is already 3.

Real-World Takeaways for Developers

  • Don’t blindly use var in loops — use let for block-level scoping.

  • Understand closures: variables are shared, not cloned.

  • These bugs are subtle — and they love to hide in callbacks, async code, or loops.

  • Interviewers use puzzles like this to see if you really understand what’s going on under the hood.

This isn’t just a trick — it’s a peek into how JavaScript really works.

Conclusion

So, did you crack it?

If you guessed 3, 3, 3 — congratulations! You’ve either fallen into this trap before or you understand how JavaScript closures and variable scoping really work. If not, don’t worry — this kind of puzzle is a rite of passage for every JavaScript developer.

What this little brain teaser shows is that writing code isn’t just about getting it to “work” — it’s about truly understanding how your language handles scope, memory, and execution timing. JavaScript can be sneaky, and puzzles like this are perfect reminders that even the simplest code can behave in unexpected ways.

Here’s the key takeaway:
Every JavaScript dev — junior or senior — benefits from slowing down and digging into how their code really works. That one variable you assumed was isolated? It’s probably being shared. That function you thought captured a moment in time? It might just be watching things change.

Next time you’re in a loop — literally or figuratively — remember what you’ve learned here.

Challenge for You

Want to take your skills even further?

  • Rewrite the puzzle using 'let' instead of 'var'.

  • Try nesting another function inside the loop. What happens?

  • Think about how this plays out in async functions — with setTimeout or Promises.

And finally:
Share this puzzle with your team or community.
Tag someone who thinks they’ve mastered JavaScript — and see if they get it right on the first try.

You survived this puzzle… but are you ready for the next one?

Subscribe for more advanced challenges, coding tips, and deep dives into JavaScript’s weird and wonderful world.

Comments (0)

No comments yet. Be the first to comment!

Leave a Comment

© 2025 Pir GeebyBytewiz Solutions