Difference between 'currying' and 'closure' in JavaScript?

So many time we got confused in currying and closure JavaScript functions just because both uses functions inside function.

So This is the answer of your question.

First I will explain What is closure in JavaScript ?

Creating a closure is nothing more than accessing a variable outside of a function's scope. Please note that a function inside a function isn't a closure. Closures are always use when need to access the variables outside the function scope.

Here is an example of closure function:

Answer will be 21. So here inside google function we are able to access the variable x which is outside the scope of the function google().

Now I will explain What is currying function in JavaScript ?

Currying is converting a single function of n arguments into n functions with a single argument each. So you can say Currying is a way to reduce functions of more than one argument to functions of one argument.

Here in Currying function we also uses the property of closures because we access the variables outside the scope of a function.

Here is the currying function example:

Conclusion: A currying function also uses the property of closure because currying function also access the variables outside the scope of a function but a currying function will have functions of n arguments with n functions while closure don't have n arguments with n functions.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *