Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript closures
var foo = function (seed) {
var currVal = seed;
var incrementor = function () {
currVal++;
console.log(currVal);
};
return incrementor;
};
var bar = foo(3);
bar(); // Logs 4
bar(); // Logs 5
bar(); // Logs 6
var baz = function (seed) {
var firstFactor = seed;
var multiplier = function (secondFactor) {
console.log(firstFactor * secondFactor);
};
return multiplier;
};
var qux = baz(3);
qux(4); // Logs 12
qux(7); // Logs 21
qux(10); // Logs 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.