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
Wednesday, July 15, 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. All third-party names, trademarks, logos, or brands referenced on our site belong to their respective owners.
Pir Gee claims no ownership over third-party intellectual property.

© 2026 Pir Gee. A Project ofTETRA SEVEN. All Rights Reserved.

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.

802

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

Fixing java.io.EOFException Error in Java ApplicationsDev Challenges

Fixing java.io.EOFException Error in Java Applications

Running into a java.io.EOFException can be frustrating, especially when your Java application looks

By: Zeenat Yasin

10 April 2026

Top 10 Real-World Programming Challenges for DevelopersDev Challenges

Top 10 Real-World Programming Challenges for Developers

In today’s software industry, knowing syntax and frameworks is only the entry ticket. What act

By: Musharaf Baig

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

In today’s fast-moving tech world, developers face constant pressure to write clean, efficient

By: Musharaf Baig

13 January 2026

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

MCP Security Checklist: How Developers Can Build Safer AI Agent Integrations

MCP Security Checklist: How Developers Can Build Safer AI Agent Integrations

By:Feroza Arshad  4 June 2026

A developer-focused MCP security checklist covering permissions, tool scopes, secrets, logging, approvals, sandboxing, and prompt-injection risks.

Read More
Agent-Ready Websites: How Developers Should Prepare Content, APIs, and Search for AI Assistants

Agent-Ready Websites: How Developers Should Prepare Content, APIs, and Search for AI Assistants

By:Feroza Arshad  4 June 2026

Learn how developers can prepare websites for AI assistants with structured content, internal search, safe APIs, permissions, and human-friendly fallbacks.

Read More
White-Collar Work Will Be Automated Soon: What Makes You So Different?

White-Collar Work Will Be Automated Soon: What Makes You So Different?

By:Feroza Arshad  1 June 2026

AI is transforming white-collar work. Discover the human skills, judgment, and value that can help professionals stay relevant in an automated future.

Read More
Using Claude Code: The Unreasonable Effectiveness of HTML

Using Claude Code: The Unreasonable Effectiveness of HTML

By:Feroza Arshad  26 May 2026

Learn how using Claude Code with HTML outputs improves readability, reporting, dashboards, and AI workflow usability.

Read More
Google Gemini 3.5 Flash: What You Need to Know

Google Gemini 3.5 Flash: What You Need to Know

By:Feroza Arshad  25 May 2026

Learn what Google Gemini 3.5 Flash is, its key features, use cases, comparisons, advantages, and whether it’s worth using in 2026.

Read More
What Google’s Generative UI Means for the Future of Search

What Google’s Generative UI Means for the Future of Search

By:Nigarish Nadeem  20 May 2026

Learn how Google Generative UI may change search behavior, SEO, website traffic, and digital visibility for brands and publishers.

Read More
Are Free Coding Tutorials Enough to Become a Developer?

Are Free Coding Tutorials Enough to Become a Developer?

By:Nigarish Nadeem  9 May 2026

Discover whether free coding tutorials are enough to become a developer, what skills matter most, and how beginners can build real-world programming experience.

Read More
The Ultimate Guide to Modern UX Design (Beginner to Pro)

The Ultimate Guide to Modern UX Design (Beginner to Pro)

By:Feroza Arshad  6 May 2026

Learn modern UX design from beginner to pro with UX principles, workflows, tools, trends, and practical career guidance.

Read More
Top AI Workflow Tools That Feel Like Having a Personal Assistant

Top AI Workflow Tools That Feel Like Having a Personal Assistant

By:Feroza Arshad  4 May 2026

Discover the best AI workflow tools that act like a personal assistant to manage tasks, emails, scheduling, and automation with ease.

Read More
Samsung Galaxy A57: The Mid-Range Phone That Feels Like a Flagship

Samsung Galaxy A57: The Mid-Range Phone That Feels Like a Flagship

By:Feroza Arshad  1 May 2026

Discover the Samsung Galaxy A57 features, performance, and price. See if this mid-range phone truly delivers a flagship-like experience.

Read More