Skip to content

Instantly share code, notes, and snippets.

@mikepenzin
Created February 28, 2017 14:59
Show Gist options
  • Save mikepenzin/4158fd029f2606d33b9df4bb7215e2d4 to your computer and use it in GitHub Desktop.
Save mikepenzin/4158fd029f2606d33b9df4bb7215e2d4 to your computer and use it in GitHub Desktop.
JavaScript Closures Example
// console.log(sum(2,3)); // Outputs 5
// console.log(sum(2)(3)); // Outputs 5
function sum(x){
if (arguments.length == 2){
// In functions arguments received as array.
// We need to check if their is two or one argument in array.
return arguments[0] + arguments[1];
} else {
return function (y) {
return x + y;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment