Three JS key concepts:
-
Functions can be nested: a child function inside a parent function (and can be returned by the parent function).
-
Lexical scope: variables from outter scope are available to inner functions (when these inner functions are created).
-
Variables which can be accessed by any function anyhow will never be garbage collected (until that function is garbage collected).
A "closure" is a place (scope) where those variables are stored at and is only available for that function who needs to access them. Except for that function, there is no other API to access/modify those variables in the closure.
In chrome dev console we can "see" closures like: