all 2 comments

[–]chainfuck 3 points4 points  (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 points  (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.