Pir Gee

Tech Tutorials
Tech News & Trends
Dev Challenges
AI & Machine Learning
Cyber Security
Developer Tools & Productivity
API's & Automation
UI/UX & Product Design
FinTech
SEO
Web 3.0
Software Comparisons
Tools & Work Flows
Sunday, March 1, 2026
Pir Gee
Pir Gee

Pir Gee is your one-stop platform for insightful, practical, and up-to-date content on modern digital technologies. Covering programming languages, databases, REST APIs, web development, and more — we bring you expert tutorials, coding guides, and tech trends to keep developers, learners, and tech enthusiasts informed, skilled, and inspired every day.

Follow us

Categories

  • Tech Tutorials
  • Tech News & Trends
  • Dev Challenges
  • AI & Machine Learning
  • Cyber Security
  • Developer Tools & Productivity
  • API's & Automation
  • UI/UX & Product Design
  • FinTech
  • SEO
  • Web 3.0
  • Software Comparisons

Policies

  • About
  • Get inTouch Pir Gee
  • Privacy Policy
  • Terms & Conditions
  • Disclaimer

Newsletter

Subscribe to Email Updates

Subscribe to receive daily updates direct to your inbox!

*We promise we won't spam you.

*All content on Pir Gee is for educational and informational purposes only.

© 2026 Pir GeebyBytewiz Solutions

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

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.

792

views


FacebookTwitterPinterestLinkedIn

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.

Tags:ConsoledevelopersJavaScriptPuzzleOutputJavaScript DeveloperJavaScript Puzzles
Zeenat Yasin

Zeenat Yasin

View profile

I am Zeenat, an SEO Specialist and Content Writer specializing in on-page and off-page SEO to improve website visibility, user experience, and performance.
I optimize website content, meta elements, and site structure, and implement effective off-page SEO strategies, including link building and authority development. Through keyword research and performance analysis, I drive targeted organic traffic and improve search rankings.
I create high-quality, search-optimized content using data-driven, white-hat SEO practices, focused on delivering sustainable, long-term growth and improved online visibility.

Related Posts

Top 10 Real-World Programming Challenges for DevelopersDev Challenges

Top 10 Real-World Programming Challenges for Developers

23 January 2026

How to Solve Coding Challenges Using GitHub Copilot and Claude AIDev Challenges

How to Solve Coding Challenges Using GitHub Copilot and Claude AI

13 January 2026

Best Weekly Coding Practice Challenges For DevelopersDev Challenges

Best Weekly Coding Practice Challenges For Developers

4 December 2025

Top 10 Coding Challenges Every Developer Should Master for InterviewsDev Challenges

Top 10 Coding Challenges Every Developer Should Master for Interviews

13 November 2025

Comments

Be the first to share your thoughts

No comments yet. Be the first to comment!

Leave a Comment

Share your thoughts and join the discussion below.

Popular News

Why E-E-A-T Signals Matter More Than Ever for Modern SEO Success

Why E-E-A-T Signals Matter More Than Ever for Modern SEO Success

23 February 2026

5 Ways Fintech Is Disrupting Traditional Banks Right Now

5 Ways Fintech Is Disrupting Traditional Banks Right Now

23 February 2026

Designing for Retention: UX Strategies That Keep Users Coming Back

Designing for Retention: UX Strategies That Keep Users Coming Back

20 February 2026

How Smart APIs Are Powering Autonomous Workflows and Bots

How Smart APIs Are Powering Autonomous Workflows and Bots

20 February 2026

AI Agents for Code Generation: A Practical Guide for Developers

AI Agents for Code Generation: A Practical Guide for Developers

13 February 2026

AI-Driven Cyber Security: The Future of Smart Threat Detection

AI-Driven Cyber Security: The Future of Smart Threat Detection

13 February 2026

How to Build a Career in AI and Machine Learning

How to Build a Career in AI and Machine Learning

23 January 2026

Top 10 Real-World Programming Challenges for Developers

Top 10 Real-World Programming Challenges for Developers

23 January 2026

Foldable Phones, AI Laptops & Smart Devices: Top Tech You Can’t Miss

Foldable Phones, AI Laptops & Smart Devices: Top Tech You Can’t Miss

21 January 2026

How to Build a Smart Support Chatbot Using Vercel AI: Step-by-Step Guide

How to Build a Smart Support Chatbot Using Vercel AI: Step-by-Step Guide

21 January 2026