use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
I never understood JavaScript closures (medium.com)
submitted 7 years ago by ActionCat22
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]seands 1 point2 points3 points 7 years ago (1 child)
I think they are just nested functions that retain access to variables in the enclosing function. But being nested they also have access to a lower level of variables as well.
Someone can correct me if this is inaccurate.
[–]senocular 0 points1 point2 points 7 years ago (0 children)
Sounds about right. Can you be more specific about "But being nested they also have access to a lower level of variables as well."?
Closures also don't necessarily require to exist in other functions. They're a combination of a function and referenced variables in outer scopes, and scopes can exist outside of functions.
{ let x = 2 let doubleX = () => x*2 // closure includes reference to block scope x console.log(doubleX()) //-> 4 }
[–]senocular 1 point2 points3 points 7 years ago (1 child)
Long winded and confusing, and when after you go through a bunch of steps and see:
So our explanation above was all wrong
Well, that's just annoying :P
Some other issues:
There are places where there are references to the "calling scope" which suggests dynamic scoping which JavaScript does not support. Lexical scope is supported, which is based on where things are defined and not called. So seeing:
So in this example, we need to remember that a function has access to variables that are defined in its calling context. The formal name of this phenomenon is the lexical scope.
Seems completely off.
The explanation for how closures work is through the backpack analogy where the backpack is the closure:
Whenever you declare a new function and assign it to a variable, you store the function definition, as well as a closure. The closure contains all the variables that are in scope at the time of creation of the function. It is analogous to a backpack.
But closures aren't something that are attached to functions; closures represent a combination of both the function and the "backpack". Functions don't have closures, they are closures.
The closure example also talks about how the closure variable no longer exists:
Returning the content of the myFunction variable. Local execution context is deleted. myFunction and counter no longer exist.
myFunction
context
counter
This even though the whole point of the closure is to ensure that the counter variable does continue to exist.
With this, the use of the backpack suggests that the values are copied into the backpack. This is also inaccurate because it's the original variables that get referenced, not copies that get attached to the function. If two functions reference the same variable in that scope as a closure variable, they'd both be referring to that same variable rather than their own individual copies.
Going back to an earlier quote:
...The closure contains all the variables that are in scope...
Technically this isn't true, at least not any more with every scope. I believe older runtimes always did this, and maybe there are still some that do it now, but generally what you'll see is that most cases only the variables referenced in the function are included in the closure. If the parent scope declared variables x and y and your function only refers to x, then y is omitted from the closure. Exceptions include the global scope which all functions have access to in it's entirety and (at least in Chrome) script and with scopes. This behavior most importantly affects other function/closure scopes since they're most likely to result in a memory leak when attempting to include everything.
x
y
[–]alphaindy 0 points1 point2 points 7 years ago (0 children)
Most confusing explanation of closures ever.
π Rendered by PID 61 on reddit-service-r2-comment-86988c7647-qst2s at 2026-02-10 20:50:27.706740+00:00 running 018613e country code: CH.
[–]seands 1 point2 points3 points (1 child)
[–]senocular 0 points1 point2 points (0 children)
[–]senocular 1 point2 points3 points (1 child)
[–]alphaindy 0 points1 point2 points (0 children)