Created
February 28, 2017 14:59
-
-
Save mikepenzin/4158fd029f2606d33b9df4bb7215e2d4 to your computer and use it in GitHub Desktop.
JavaScript Closures Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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