Javascript Closure tutorial ( Closures Explained )
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about local variables staying accessible after a function returns and how closures behave behind the scenes.
Recommended by 2 people across 2 different months, 2017 to 2018.
What it is
A 12-minute tutorial by techsith explaining JavaScript closures, covering how an inner function remembers the environment in which it was created and where closures are useful. It walks through lexical scope with code examples.
What people said
Strong rec sajan · 2017-07 · Stack Overflow · on “Local variable inside function is accessed outside the function after the function has executed. How does this happen?”
It is called closures. You can watch this video [this video] to get understand how it works, behind the scene. It is explained extremely good.
Recommends Bimal Das · 2018-01 · Stack Overflow · on “How to ensure closures created in a loop capture the value of the loop variable from their particular iteration?”
We will check , what actually happens when you declare var and let one by one. Case1 : using var <script> var funcs = []; for (var i = 0; i < 3; i++) { funcs[i] = function () { debugger; console.log("My value: " + i); }; } console.log(funcs); </script> Now open your chrome console window by pressing F12 and refresh the page. Expend every 3 functions inside the array.You will see an property called [[Scopes]] .Expand that one. You will see one array object called "Global" ,expand that one. You will find a property 'i' declared into the object which having value 3. [LINK] [LINK] Conclusion: When you declare a variable using 'var' outside a function ,it becomes global variable(you can check by
Cites Sandeep Gupta · 2017-11 · Stack Overflow · on “Code working differently when run in jsfiddle”
@JamesThorpe I see that its JSfiddle issue. I am following this [this video] tutorial which says above function forms closure. Can someone give a definitive answer if its true or not. Thanks.