<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pratik Soni</title>
    <description>The latest articles on DEV Community by Pratik Soni (@pratik098).</description>
    <link>https://dev.to/pratik098</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F814420%2F17a51b13-43a7-4b7e-a0d1-f577cd7ba737.webp</url>
      <title>DEV Community: Pratik Soni</title>
      <link>https://dev.to/pratik098</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pratik098"/>
    <language>en</language>
    <item>
      <title>Currying in JavaScript</title>
      <dc:creator>Pratik Soni</dc:creator>
      <pubDate>Thu, 05 Feb 2026 11:07:57 +0000</pubDate>
      <link>https://dev.to/pratik098/currying-in-javascript-jhd</link>
      <guid>https://dev.to/pratik098/currying-in-javascript-jhd</guid>
      <description>&lt;p&gt;When I first learned JavaScript, concepts like currying felt confusing and unnecessary—until I actually started using it.&lt;/p&gt;

&lt;p&gt;Think about ordering coffee. You don't shout everything at once.&lt;br&gt;
You decide step by step — first, you choose the type of coffee, then the size, and finally the milk.&lt;/p&gt;

&lt;p&gt;The same idea applies to currying in JavaScript.&lt;br&gt;
Currying allows us to call a function like this:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;orderCoffee('Latte')('Medium')('Almond milk');&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Each function call captures one piece of information and returns control to another function, which waits for the next input.&lt;/p&gt;

&lt;p&gt;Let’s look at a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function orderCoffee(type) {
  return function (size) {
    return function (milk) {
      return `You selected ${type} ${size} with ${milk}`;
    };
  };
}

const orderOne = orderCoffee('Latte')('Medium')('Almond milk');
const orderTwo = orderCoffee("Americano")('Large')('No milk');
const orderThree = orderCoffee('Mocha')('Medium')('Almond milk');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Currying breaks a function into smaller functions, each taking one argument, and allows you to reuse behavior instead of repeating code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s break down what happens internally.
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Step 1: const step1 = orderCoffee('Latte')
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;type = 'Latte'&lt;/li&gt;
&lt;li&gt;Returns a function&lt;/li&gt;
&lt;li&gt;That function remembers the type via closure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now the function looks like this internally&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function (size) {
  return function (milk) {
   return `You selected Latte ${size} with ${milk}`;
  };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Step 2: const step2 = step1('Medium')
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;size = 'Medium'&lt;/li&gt;
&lt;li&gt;Returns another function&lt;/li&gt;
&lt;li&gt;Remembers size and type via closure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now the function becomes&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function (milk) {
  return `You selected Latte Medium with ${milk}`;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Step 3: const finalStep = step2('Almond milk')
&lt;/h4&gt;

&lt;p&gt;Once all arguments are provided, the function returns the final result:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const orderOne = orderCoffee('Latte')('Medium')('Almond milk');

Output:
You selected Latte Medium with Almond milk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Now let's see currying in an arrow function
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const orderCoffee = (type) =&amp;gt; (size) =&amp;gt; (milk) =&amp;gt;
  `You selected ${type} ${size} with ${milk}`;

const order = orderCoffee('Latte')('Medium')('Almond milk');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Arrow functions make currying look cleaner, but the internal working remains the same — let’s break it down.&lt;/p&gt;

&lt;h4&gt;
  
  
  Execution Flow
&lt;/h4&gt;

&lt;p&gt;Step 1: order = orderCoffee('Latte')&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Returns a function waiting for size.
Now the string becomes: &lt;code&gt;You selected Latte ${size} with ${milk}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 2: order = orderCoffee('Latte')('Medium')&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Returns a function waiting for milk.
Now the string becomes: &lt;code&gt;You selected Latte Medium with ${milk}.&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 3: order = orderCoffee('Latte')('Medium')('Almond milk')&lt;br&gt;
All arguments are provided, and the final string is returned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;order = orderCoffee('Latte')('Medium')('Almond milk')

Output
You selected Latte Medium with Almond milk. 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Currying is a functional programming technique that transforms a function with multiple parameters into a sequence of functions like (f(a,b,c) ~~~~&amp;gt; f(a)(b)(c)), each accepting a single argument. Currying relies heavily on closures. Each nested function retains access to the arguments provided in previous calls, allowing values to be accumulated until all required parameters are available. Once the expected number of arguments is collected, the original function executes and returns the final result.&lt;/p&gt;

&lt;p&gt;That’s it for this article — see you in the next blog!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
