Up to date
Published
5 min read

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

Understanding JavaScript Closures With Examples

Closures are essential for creating functions that maintain state, without relying on global variables.

I came across an interesting question on Reddit. Someone was having trouble understanding why two different approaches to counting values in JavaScript didn’t behave the same way.

The Reddit user expected that two similar ways of incrementing a counter would reset the count to zero, but they didn’t.

The Reddit user was confused. They thought the first and second approach should behave the same way, but they don’t. The first approach resets the count to zero every time, while the second approach continues from where it left off.

Why Does This Happen?

The key difference lies in closures. If that word makes your head spin, don’t worry—I’ll break it down.

A closure in JavaScript happens when a function can “remember” and access variables from the outer scope, even after that outer function has finished executing. This allows the inner function to maintain access to the variables in place when the function was created.

  • outerFunction() is called, which creates a local variable outerVariable and defines an innerFunction.
  • outerFunction() then returns innerFunction, but here’s the magic part: even though outerFunction has finished running, the innerFunction can still access outerVariable. This is because of closure.

The innerFunction “remembers” the environment where it was created, which includes the outerVariable variable. When you call closureFunction(), it can still access and log the outerVariable even though the outerFunction() has completed execution.

Simple Counter Using Closures

  • createCounter() defines a local count variable and returns a function that increments and logs the value of count.
  • Even after createCounter() finishes, the returned function still has access to the count variable thanks to closure. So, every time you call counter(), it updates the same count variable, preserving the value between calls.

Closure with Parameters

Let’s expand the counter to take an initial value as a parameter.

  • This time, we pass an initial value (startValue) to createCounter(). The count variable is initialized with this value.
  • When you create two counters (counterFrom10 and counterFrom5), each has its independent count variable, but both preserve their state between calls. Each closure remembers its starting value.

Multiple Functions Sharing the Same Closure

Now, let’s say you want to create a counter that can increment, decrement, or reset the count. Here’s how you can do that using closures.

  • We’re returning an object that contains multiple functions, all of which share access to the same count variable.
  • Each method (increment, decrement, reset) is a closure, and they all share the same count state. So, whether you increment, decrement, or reset, they’re all working with the same count variable that’s kept alive by the closure.

First Approach: Why Does It Reset?

In the first approach:

Every time you call createCount(), you are creating a new instance of the count variable.

  • The first createCount() call creates a fresh count = 0, increments it to 1, and logs it.
  • The second createCount() call does the same thing: it creates a new count = 0 and increments it again to 1.

This is why the count resets in this approach—it’s like hitting the “reset button” every time you call createCount().

Second Approach: Why Does It Continue?

Now, look at the second approach:

Here, you’re only calling createCount() once. This is crucial because you’re saving the result (which is the increment function) into the counter variable.

  • The first time you call counter.increment(), it uses the count that was initialized as 0 in the original createCount(). It increments the count to 1.
  • The second time you call counter.increment(), it doesn’t reset count. It just uses the same count value that’s now 1 and increments it to 2.

Since you’re not calling createCount() again, you’re using the same closure, and the count value persists between calls.

Closures are super useful because they let you “remember” values in your functions, even after those functions have finished running. In real-world applications, closures are the backbone of things like:

  • Event listeners that need to remember some information.
  • Callbacks where the function is called much later but still needs access to earlier variables.
  • Private variables, where you want to keep a value hidden from the outside world.

Found this article helpful? You might enjoy my free newsletter. I share dev tips and insights to help you grow your coding skills and advance your tech career.

Interested in supporting this blog in exchange for a shoutout? Get in touch.


Liked this post?

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

Javascript
4 min read

JavaScript compile hints: what they are and when to use them

V8's compile hints let you control which JavaScript gets compiled immediately during page load

May 12, 2025
Read article
Javascript
4 min read

Error.isError(): A Better Way to Check Error Types in JavaScript

Why the new Error.isError() method solves important cross-realm issues and provides more reliable error identification than instanceof

May 9, 2025
Read article
Javascript
5 min read

Precise Decimal Math in JavaScript with Fraction.js

How to handle exact decimal calculations in JavaScript when floating-point precision isn't good enough

Nov 16, 2024
Read article
Javascript
6 min read

AggregateError in JavaScript

Handle multiple errors at once

Sep 2, 2024
Read article
Javascript
4 min read

JavaScript Import Attributes (ES2025)

Understanding the new import attributes syntax and why we can't rely on file extensions alone

Nov 10, 2024
Read article
Javascript
6 min read

setImmediate() vs setTimeout() in JavaScript

both setImmediate() and setTimeout() are used for scheduling tasks, but they work differently.

Sep 8, 2024
Read article
Javascript
3 min read

Float16Array in JavaScript

Understanding the new 16-bit floating point array in JavaScript

Apr 14, 2025
Read article
Javascript
4 min read

Intl.DurationFormat: Format Time Durations with Locale Support

Stop writing manual duration formatting code. Instead, leverage the new powerful Intl.DateTimeFormat API for internationalized time displays

Mar 13, 2025
Read article
Javascript
4 min read

Embrace Intermediate Variables and Early Returns in JavaScript

Early returns and intermediate variables make your code easier to reason about

Aug 30, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/understanding-javascript-closures. It was written by a human and polished using grammar tools for clarity.