Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created April 25, 2016 13:29
Show Gist options
  • Save ChrisMoney/d34046e8f2f265b243f479d7f0d08ac7 to your computer and use it in GitHub Desktop.
Save ChrisMoney/d34046e8f2f265b243f479d7f0d08ac7 to your computer and use it in GitHub Desktop.
Javascript - Closure
var add = (function () {
var counter = 0;
return function () {return counter += 1;}
})();
add();
add();
add();
// the counter is now 3
@ChrisMoney
Copy link
Author

ChrisMoney commented Sep 26, 2018

Example Explained
A closure is a variable assigned to a function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment