<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Currying in Javascript]]></title><description><![CDATA[Currying in Javascript]]></description><link>https://curryinginjs.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 26 Nov 2026 13:38:48 GMT</lastBuildDate><atom:link href="https://curryinginjs.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Currying in Javascript]]></title><description><![CDATA[Learn the fundamentals of currying in JavaScript, its implementation, and applications for function composition in this beginner-friendly guide]]></description><link>https://curryinginjs.hashnode.dev/currying-in-javascript</link><guid isPermaLink="true">https://curryinginjs.hashnode.dev/currying-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[Developer]]></category><dc:creator><![CDATA[Tiasha Bhowmik]]></dc:creator><pubDate>Tue, 05 Nov 2024 07:58:21 GMT</pubDate><content:encoded><![CDATA[<p>In this blog, the reader will learn about the basics of <code>currying</code> in JavaScript. The excitement that comes with hearing about <code>curry food</code> is similar to my enthusiasm for explaining the fundamentals of currying and its implementation in Vanilla JS. Let's get started .</p>
<h2 id="heading-introduction"><strong>Introduction:</strong></h2>
<p>As per several references, currying can be seen as one of the new ways to write functions. Basically it involves transformation of a function that takes several parameters into a series of smaller functions that take lesser number of parameters. In other words if we have a function <code>add</code> that requires the use of 3 parameters to compute the sum of those 3 parameters then after applying currying that same function would require the use of those 3 parameters but in a different fashion. Let's see it below</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b, c</span>) </span>{
    <span class="hljs-keyword">return</span> a + b + c;
}
<span class="hljs-keyword">let</span> sumOf234 = add(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
<span class="hljs-built_in">console</span>.log(sumOf234) <span class="hljs-comment">//10</span>
</code></pre>
<p>This is a simple function that we wrote to compute the sum of 3 numbers. Now let's use currying to rewrite this add function.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">firstNum</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">secondNum</span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">thirdNum</span>) </span>{
            <span class="hljs-keyword">return</span> firstNum + secondNum + thirdNum;
        }
    }
}
</code></pre>
<p>As you can see we have started breaking the function to take lesser number of parameters and generate new functions that have their parameter requirements which further compute the results.</p>
<p>Now you might be wondering that how the innermost function might have access to the parameters passed to the outer functions. That brings us to another concept called as Closures which explains about the innermost function's access to the outside environment. The definition from <strong>MDN</strong> can be seen as:</p>
<blockquote>
<p><strong><em>A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.</em></strong></p>
</blockquote>
<p>Here is an example demonstrating closures</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">makeFunc</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> name = <span class="hljs-string">'Currying'</span>;
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">displayName</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(name);
  }
  <span class="hljs-keyword">return</span> displayName;
}

<span class="hljs-keyword">const</span> myFunc = makeFunc();
myFunc();

<span class="hljs-comment">//as soon as we invoke myFunc, </span>
<span class="hljs-comment">//the function would get executed</span>
<span class="hljs-comment">// we would get to see Currying on the console</span>
</code></pre>
<p>So once the currying function gets made how can we invoke it or call it to receive the value? There are two ways to do so:</p>
<ol>
<li>One is by directly passing the parameters to the main function. For example in this case:</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-built_in">console</span>.log(add(<span class="hljs-number">2</span>)(<span class="hljs-number">3</span>)(<span class="hljs-number">4</span>));
<span class="hljs-comment">//4</span>
</code></pre>
<ol start="2">
<li>Other is by creating temporary variables that store the functions and pass the parameters one by one while making them. This example might help you understand:</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> add2=add(<span class="hljs-number">2</span>);
<span class="hljs-comment">// add2 is a variable which stores a function </span>
<span class="hljs-comment">// which will take 2 parameters further to return the result</span>
<span class="hljs-comment">// In case you want to invoke it directly can write this</span>
<span class="hljs-comment">// add2(3)(4);</span>

<span class="hljs-comment">// else you can create the function expressions more to use it in simple format</span>
<span class="hljs-keyword">const</span> add23=add(<span class="hljs-number">2</span>)(<span class="hljs-number">3</span>);

<span class="hljs-comment">// add23 is a variable which stores a function that will take 1 parameter further</span>
<span class="hljs-comment">// to return the result</span>
<span class="hljs-keyword">const</span> result=add(<span class="hljs-number">2</span>)(<span class="hljs-number">3</span>)(<span class="hljs-number">4</span>)
</code></pre>
<p>These small functions stored within variables from the original functions are also called as <strong>partially curried functions</strong>.</p>
<p>Implementing currying through arrow functions becomes much more easier to understand. Here is how it looks after using arrow functions:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> add = <span class="hljs-function"><span class="hljs-params">num1</span>=&gt;</span><span class="hljs-function"><span class="hljs-params">num2</span>=&gt;</span><span class="hljs-function"><span class="hljs-params">num3</span>=&gt;</span>num1+num2+num3;
consoel.log(add(<span class="hljs-number">2</span>)(<span class="hljs-number">3</span>)(<span class="hljs-number">4</span>))

<span class="hljs-comment">// Would work the same as above</span>
</code></pre>
<p>Till now you must have got a clear idea of how to implement currying if required in the functions. Like while making the add function (can see the code snippet example where partially curried functions are stored in variables):</p>
<p>As soon as the <code>add</code> function is invoked for the first time then it stores a functional wrapper that takes <code>secondNum</code> as parameter inside the variable <code>add2</code> and in the same way the variable <code>add23</code> stores a functional wrapper that would take <code>thirdNum</code> as parameter and give us the value of the sum.</p>
<h2 id="heading-applications-of-currying">APPLICATIONS OF CURRYING:</h2>
<p>Suppose if we have multiple functions that take the same number of parameters and return different results based on the actions executed in those functions, then instead of calling those functions one by one by as well as passing the arguments inside it every time the functions are called is there a way to pass all the functions at once and even provide the arguments to it thus getting the result instantly?</p>
<p>Yes there is a way and that can be achieved through <strong>Currying</strong>.</p>
<p>Let's first understand through a example what I am trying to do as explained above. Suppose I have 3 functions that perform the mathematical operations of addition, subtraction &amp; multiplication of 3 numbers which are passed to it as parameters.</p>
<p>Now if I want to execute a particular mathematical operation on all the 3 numbers and pass its result as one of the parameters with the remaining 2 numbers as parameters to the functions which are called afterwards;</p>
<p>Here is the code of all the functions indicating the mathematical operations:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Mathematical operations written as functions to be executed on 3 numbers</span>

<span class="hljs-keyword">const</span> addition = <span class="hljs-function">(<span class="hljs-params">num1,num2,num3</span>)=&gt;</span>num1+num2+num3;
<span class="hljs-keyword">const</span> subtraction = <span class="hljs-function">(<span class="hljs-params">num1,num2,num3</span>)=&gt;</span>num1-num2-num3;
<span class="hljs-keyword">const</span> multiplication = <span class="hljs-function">(<span class="hljs-params">num1,num2,num3</span>)=&gt;</span>num1*num2*num3;
</code></pre>
<p>As explained , if one of the functions is being executed first on all the 3 numbers and its return value is passed to the other functions with the remaining 2 numbers to the functions yet to be called.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Calling the functions one by one</span>
<span class="hljs-comment">// with the first function's return value passed to the second function call</span>
<span class="hljs-comment">// which is further being passed to the final function</span>

<span class="hljs-keyword">const</span> sumResult = addition(<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>);
<span class="hljs-keyword">const</span> differenceResult = subtraction(addResult,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>);
<span class="hljs-keyword">const</span> productResult = multiplication(differenceResult,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>);

<span class="hljs-built_in">console</span>.log(productResult);
<span class="hljs-comment">//40</span>
</code></pre>
<p>There is another way of executing the function call which looks difficult to understand at first but will get clear</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> cumulativeResult=multiplication(subtraction(addition(<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>),<span class="hljs-number">4</span>,<span class="hljs-number">5</span>),<span class="hljs-number">4</span>,<span class="hljs-number">5</span>);
<span class="hljs-built_in">console</span>.log(cumulativeResult);
<span class="hljs-comment">// Using the result of one function and passing it as an argument to the corresponding </span>
<span class="hljs-comment">// function calls</span>
</code></pre>
<p>Now this way might seem shorter of using the result of one function and passing it as an argument to the other function but not much readable and easy to understand to others.</p>
<p>That is where currying can assist us in simplifying the process of calling these functions together and pass the parameters all at once maintaining the readability. It lets us perform <code>function composition</code> i.e. providing a set of functions that will be called in sequence by passing the results of one function as an argument to the next function. Let's witness the magic here.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> generateResult = <span class="hljs-function">(<span class="hljs-params">...participatingFunctions</span>) =&gt;</span> <span class="hljs-function">(<span class="hljs-params">a, b, c</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> value = c;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; participatingFunctions.length; i++) {
    value = participatingFunctions[i](value, a, b);
  }
  <span class="hljs-keyword">return</span> value;
};

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Currying and composition together: "</span>,generateResult3(add, subtract, multiply)(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>));
</code></pre>
<p>We have created a function called as <code>generateResult</code> that will first take the functions which will operate on the numbers one by one and returns us a function that will take the numbers on which the mathematical operations will be carried out accordingly.</p>
<p>This is one of the applications of currying to perform function composition too.Real life examples of this can be seen in an:</p>
<ul>
<li>Ecommerce marketplace: We have a page on the ecommerce application that displays us the list of products available on the platform. In order to find our desired product we have different types of filters and sorting facilities available. On marking/unmarking those filters and sorts we get the desired product. Function composition can be used here to take all the different types of sorting and filtering functions and apply it on the products data depending on the sorts and filters applied here</li>
</ul>
<p>#javascript</p>
]]></content:encoded></item></channel></rss>