#1 India's Top IT Training Institute

×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
🔒 FREE · STEP BY STEP

JavaScript Closures Visualizer: See What Functions Remember

Choose an example and step through the code line by line, with every scope and the console shown.

  • 📦 Scopes as boxes
  • 🔁 var vs let
  • 🖨️ Console output

How to use the JavaScript Closures Visualizer

  1. 1. Set it upPick one of four examples.
  2. 2. RunPress Step through.
  3. 3. ReplayEach step highlights a line and shows the live scopes, each with an arrow to its outer scope.

Worked example: the counter factory

makeCounter creates count = 0 and returns an inner function that increments it. Calling makeCounter twice creates two separate scopes, so a and b each have their own count. The console shows 1, 2, 1: a() prints 1, then 2, and b() prints 1, not 3. The key moment is step 3: makeCounter has returned, but its scope stays alive because the returned function still refers to count. That surviving scope is the closure.

The loop gotcha

In the var vs let example, a for loop with var schedules three timeouts that all print var 3, because they share the single i, which is 3 by the time they run. The same loop with let prints let 0, let 1, let 2, because let creates a fresh binding on every iteration. Interviewers love this question; the visual shows three let scopes against one var.

What the other examples show

  • once(): the wrapper remembers done and result, so the function runs only the first time; both calls print 20.
  • Private data: balance lives only in the closure, so acc.balance is undefined, while deposit and getBalance can still reach it.
  • Each example's output was checked by running the code in Node.js, so what you see in the console panel is exactly what JavaScript prints.

Frequently asked questions

Can I type my own code?

No. Running arbitrary code would need eval, which this site avoids. Copy an example and try changes in your browser console.

Are closures only a JavaScript thing?

No. Python, Java lambdas, C# and many other languages have closures.

Do closures cause memory leaks?

They keep variables alive as long as the function is reachable. That's usually fine, but large data captured by long-lived listeners can waste memory.

Is the JavaScript closures visualizer free?

Yes. You can use the JavaScript closures visualizer without signing up. The first time you run it, we ask once for your name and WhatsApp number; after that the JavaScript closures visualizer and every other Uncodemy tool open straight away.

More free tools

See all free tools