Basics

What is a closure in JavaScript — and why your counter is stuck at 1

Illustration: a function sets off with a backpack full of its variables

You asked AI for a stopwatch in React. You run it. The screen says 1. You wait. Still 1. A minute later — still 1.

The code looks honest: once a second the timer adds one. No errors in the console. The timer really is ticking — it just keeps adding one to the same zero.

The culprit is a closure. You use closures every day without knowing the name. Let's figure it out, and in five minutes you'll spot this bug at a glance.

What a closure is

A closure is a function plus the variables it remembered from the place it was created.

Here's a simple example:

function makeCounter() {
  let count = 0
  return () => {
    count = count + 1
    return count
  }
}

const next = makeCounter()
next() // 1
next() // 2

makeCounter has already finished. By rights, its count variable should be gone. But the inner function holds on to it — and every next() call sees the very same count.

A handy picture: the function goes out into the world with a backpack. Inside are the variables of the scope where it was born. Wherever the function is passed, the backpack travels with it.

What exactly is in the backpack

First subtlety. The backpack holds the variable itself, not a snapshot of its value.

let price = 100
const show = () => price
price = 200
show() // 200, not 100

The function looks at the live variable. Change price — the function sees the new value.

The second subtlety matters more. If the variable is created again, a new function gets a new backpack. The old function keeps holding the old variable.

That's exactly what happens in React.

That stuck counter

Here's the stopwatch code AI often writes:

const [count, setCount] = useState(0)

useEffect(() => {
  const id = setInterval(() => {
    setCount(count + 1)
  }, 1000)
  return () => clearInterval(id)
}, [])

Step by step:

  1. First render. count is 0.
  2. useEffect with an empty [] runs once. It creates the timer function. Into its backpack goes the count of this render — the one that equals 0.
  3. A second later the timer calls setCount(0 + 1). The screen shows 1.
  4. React renders the component again. A new count appears, equal to 1. But the timer doesn't know — its backpack still holds the old zero.
  5. Another second: setCount(0 + 1) again. 1 again. Forever.

Every render in React is a fresh call of the component function. That means fresh variables. And state is a snapshot at render time, not a live cell.

This trap is called a stale closure.

How to fix it

The most reliable way: don't read count from the backpack at all. Pass a function to setCount:

setCount(c => c + 1)

React plugs in the fresh value itself. The timer no longer needs to remember anything.

The second way: add count to the dependencies [count]. Then the effect recreates the timer on every change. It works, but the timer keeps resetting. For a stopwatch, the first way is better.

Where you meet closures every day

  • Click handlers. onClick={() => remove(item.id)} — the arrow remembered the item of its own list row. That's why each button removes its own item.
  • Timers and delays. Everything you pass to setTimeout leaves with a backpack. Hence stuck values.
  • Private data. In makeCounter there's no way to reach count from outside — only through next(). That's how you hide state without classes.
  • Function factories. debounce, once, memoization — all functions that return functions with a backpack.

Almost every callback you hand off somewhere is a closure.

How to tell AI about it

Models often fix this bug at random: they add useRef, a second effect, flags. Describe the mechanism and it'll hit the target right away.

Weak promptthe counter doesn't go up, fix it
Strong promptA React counter is stuck at 1. A setInterval timer is created in useEffect with empty dependencies and calls setCount(count + 1). Looks like a stale closure: the timer function remembers count from the first render. Fix it with a functional update setCount(c => c + 1), no useRef and no recreating the timer. Show only the changed lines.

Is a closure the same thing as scope?

No, but they're related. Scope decides which variables are visible at a given place in the code. A closure is when a function carries those visible variables away and uses them later.

Is it true that closures "leak" memory?

Sometimes. As long as the function is alive, so is its backpack. If a handler holds a huge array and nobody removes it, the array won't be freed. Remove your listeners and timers — and there's no problem.

Do Python and other languages have closures?

Yes, almost anywhere a function can return a function: Python, Swift, Kotlin, Go. Same idea — the function remembers where it was born.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →