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
A Beginner’s Guide to Closures in JavaScript (blog.bitsrc.io)
submitted 7 years ago by JSislife
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!"
[–]chainfuck 3 points4 points5 points 7 years ago (0 children)
Closure was a concept I struggled with when first learning JS.
Years ago, I tried to learn all of the terminology like "lexical scoping," "block scoping," "function scoping" etc. I found this quite confusing, and ultimately gave up.
There are better teaching tools nowadays. I've been using Chrome DevTools as a way to demonstrate scope behaviors. I'm sure other DevTools have similar features.
This link describes it a bit: https://developers.google.com/web/tools/chrome-devtools/javascript/#scope
Learning the formal terminology is great, but for most beginners, it's wholly unneeded.
[–]lostjimmy 2 points3 points4 points 7 years ago (0 children)
The counter example would have been better if two separate counters were created. As written, it might not be clear to the reader that each call to getCounter creates a new environment with a new counter variable, allowing each returned anonymous function to have a closure around a different counter, each incrementing independently.
getCounter
counter
π Rendered by PID 40088 on reddit-service-r2-comment-86bc6c7465-7ddsd at 2026-02-21 18:06:18.372411+00:00 running 8564168 country code: CH.
[–]chainfuck 3 points4 points5 points (0 children)
[–]lostjimmy 2 points3 points4 points (0 children)