<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arun kumar on Medium]]></title>
        <description><![CDATA[Stories by Arun kumar on Medium]]></description>
        <link>https://medium.com/@arunkumara300?source=rss-50eb9fa0a56f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HmFGpBa3Hxl828I7</url>
            <title>Stories by Arun kumar on Medium</title>
            <link>https://medium.com/@arunkumara300?source=rss-50eb9fa0a56f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:11:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arunkumara300/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Closures and Currying in JavaScript]]></title>
            <link>https://arunkumara300.medium.com/closures-and-currying-in-javascript-24c0a86c66a9?source=rss-50eb9fa0a56f------2</link>
            <guid isPermaLink="false">https://medium.com/p/24c0a86c66a9</guid>
            <category><![CDATA[currying]]></category>
            <category><![CDATA[closure]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[interview]]></category>
            <dc:creator><![CDATA[Arun kumar]]></dc:creator>
            <pubDate>Sat, 05 Dec 2020 16:06:53 GMT</pubDate>
            <atom:updated>2020-12-05T16:06:53.411Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LCaQKDX8KTu_HUv5" /><figcaption>Photo by <a href="https://unsplash.com/@zenking?utm_source=medium&amp;utm_medium=referral">Rob King</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Functions</h3><pre>function myFn() {<br>    console.log(&#39;I am a function&#39;);<br>}<br>myFn();</pre><p>functions are the super interesting things in javascript. They are handy!. They let us declare our variables inside its scope, without polluting the whole code-base / component. They perform “<strong>a</strong>” task, and can be able to perform the same action again and again as much as we need. Functions can contain functions, return functions, and can take other functions as its arguments. We will see an important concept of function in javascript, The <strong>Closure</strong>.</p><h3>Closure</h3><pre>var globalVariable = 1;<br>function <strong>parentFn</strong>() {  <br>    var myVar = 10;<br>    function <strong>myClosure</strong>() {<br>        console.log(myVar);<br>    }<br>    <strong>myClosure</strong>();  // 10<br>}</pre><p>A closure is a function, which is <strong>enclosed</strong> <strong>inside a function</strong> and it can even be nested inside functions. But the scope will be the closure’s scope, and as well as it&#39;s surrounding (<strong>lexical scope) parent’s scope.</strong></p><p>Now our closure is private to the function ‘parent’ and it is not accessible outside.</p><p>But our closure can access its parent’s variables, and what else is accessible by its parent i.e. in our case, ‘myClosure’ can access myVar and also globalVariable.</p><h3>Returning a closure</h3><p>Ok, can you try to guess what will happen, if <strong>myClosure </strong>is returned from the parent function?</p><pre>var globalVariable = 1;</pre><pre>function <strong>parentFn</strong>() {  <br>    var myVar = 10;<br>    function <strong>myClosure</strong>() {<br>        console.log(myVar);<br>    }<br>    return <strong>myClosure</strong>;<br>}</pre><pre>let myReturnedClosure = <strong>parentFn</strong>();</pre><p>yes, you are right, <strong>myReturnedClosure </strong>contains the function myClosure. But what happens to <strong>myVar</strong> , since we are just returning the function.</p><pre>myReturnedClosure(); // 10</pre><p>The closure will contain reference to all the variables, whatever is being used by that closure. So in our case, <strong>myVar </strong>will not be garbage collected, after the execution of <strong>parentFn </strong>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop">event loop</a>)<strong>. </strong>As I mentioned, <strong>functions</strong> are the super interesting things in javascript.</p><h3>Currying</h3><p>Currying is nothing but a returned closure, which may take arguments in the future <strong>one at a time.</strong></p><pre>function <strong>makeAdder</strong>(num1) {<br>    return function(num2) {<br>        return num1 + num2;<br>    }<br>}</pre><pre>let adder5 = <strong>makeAdder</strong>(5);<br>let adder10 = <strong>makeAdder</strong>(10);</pre><pre>console.log(<strong>adder5</strong>(5)); // 10<br>console.log(<strong>adder10</strong>(5)); // 15</pre><p>seems cool right? <strong>makeAdder </strong>takes one argument num1 and returns a function, which can take a new argument in the future. <strong>makeAdder</strong>(5) return as function, which may look like,</p><pre><strong>makeAdder</strong>(5);</pre><pre>returns</pre><pre> function(num2) {<br>     return 5 + num2;<br> }</pre><p>so when we invoke the returned function with a new argument, it results in the expected result of returning 5 + num2.</p><pre>function <strong>adder5</strong>(num2) {<br>    return 5 + num2;<br>}</pre><pre><strong>adder5</strong>(10); //  return 5 + 10; =&gt; 15</pre><p><strong>Currying in practical</strong></p><p>we can use this method for creating a function, which is already being initialized with the Api and the type, capable of taking just the payload and makes the Api call.</p><pre>function <strong>initAPi</strong>() {<br>    let <strong>myApi</strong> = &quot;https://api.my-api.in/&quot;;<br>    return <strong>function</strong> (size) {<br>        fetch(`${myApi}${size}`).then((response) =&gt; {<br>            console.log(response);<br>         }<br>    }<br>}</pre><pre>let<strong> getDataOfRequiredSize </strong>= initAPi<strong>();<br></strong>getDataOfRequiredSize<strong>(10)</strong>; // gives results of size 10.</pre><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24c0a86c66a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>