<?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: shoaib zaki</title>
    <description>The latest articles on DEV Community by shoaib zaki (@shoaibzaak).</description>
    <link>https://dev.to/shoaibzaak</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%2F843457%2F8b052354-1fe4-438f-a448-43ac4cb764a4.jpeg</url>
      <title>DEV Community: shoaib zaki</title>
      <link>https://dev.to/shoaibzaak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shoaibzaak"/>
    <language>en</language>
    <item>
      <title>Closures</title>
      <dc:creator>shoaib zaki</dc:creator>
      <pubDate>Fri, 08 Apr 2022 07:30:47 +0000</pubDate>
      <link>https://dev.to/shoaibzaak/closures-1hb2</link>
      <guid>https://dev.to/shoaibzaak/closures-1hb2</guid>
      <description>&lt;p&gt;A closure is a function that is bundled together with its lexical environment. in other words we can say that it is a function that has excess to its outer scope&lt;/p&gt;

&lt;p&gt;Let us see a simple example to clear about lexical scoping first&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function outer(){
var a='success'   local scope variable of the outer()
  function inner(){
   closure     alert(a)   it show the alert page with       
  success word which is from the outer variable    
}
inner()
}

outer()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;strong&gt;outer() function&lt;/strong&gt; creates the &lt;strong&gt;inner() function&lt;/strong&gt;.&lt;strong&gt;Inner()&lt;/strong&gt; has access to the outer scope variable. You can see from the above example &lt;strong&gt;inner() function&lt;/strong&gt; has not their local variable but when the &lt;strong&gt;outer() function&lt;/strong&gt; is invoked then the alert statement inside the &lt;br&gt;
&lt;strong&gt;inner function&lt;/strong&gt; has a variable destination this is called the &lt;strong&gt;lexical scoping&lt;/strong&gt;.In lexical scoping inner function has the access to the outer function that we have seen now. &lt;/p&gt;

&lt;p&gt;Now we see another 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 newFunc() {
  var name = 'success';
  function displayName() {
    alert(name);
  }
  return displayName;
}

var myfunc = newFunc();
myfunc();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This has the same output as the previous one. The difference is that&lt;br&gt;&lt;br&gt;
 &lt;strong&gt;displayName()&lt;/strong&gt; is returned by the &lt;strong&gt;new Func()&lt;/strong&gt; before execution .This quite looks unintuitive because in most languages local variable is excess to its local scope but the local variable of the &lt;strong&gt;newFun()&lt;/strong&gt; has excessed by the &lt;strong&gt;display name()&lt;/strong&gt; the reason is that function in the javascript forms the closures as said earlier closure is the combination of a function and the lexical environment within which that function was declared. This environment consists of any local variables that were in-scope at the time the closure was created.In the case above &lt;strong&gt;myfunc&lt;/strong&gt; is a reference to the instance of &lt;strong&gt;displayName()&lt;/strong&gt; that is created when the &lt;strong&gt;new Func()&lt;/strong&gt; is run.And displayName() is a reference to the instance of its lexical environment that's why when the new &lt;strong&gt;new Func()&lt;/strong&gt; is invoked we have an alert with name success in our output &lt;/p&gt;

&lt;p&gt;Now we are going to see the new interesting 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 makeAdder(x) {
  return function(y) {
    return x + y;
  };
}

var add5 = makeAdder(6);
var add10 = makeAdder(7);

console.log(add5(2));  // 8
console.log(add10(10)); // 17

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;makeAdder()&lt;/strong&gt; is a function with parameter x which is returned function with parameter y and this will return the sum of both parameters x &amp;amp; y.The &lt;strong&gt;makeAdder()&lt;/strong&gt; is function factory which creates the functions which have values for the parameter x,y.The function factory has two functions one adds 6 to its parameter and one is adding the 7 to its parameter. add5 and add10 both are closures with the same functional body with different lexical environments add5's lexical environment x is 5 and the add10's lexical environments x is 10 due to different values of x's they have the different lexical environments.When the &lt;strong&gt;makeAdder(6)&lt;/strong&gt;  is invoked &lt;strong&gt;add5&lt;/strong&gt; is referenced to the instance  &lt;strong&gt;function(y)&lt;/strong&gt; which will be run after the execution of the &lt;strong&gt;makeAdder(6)&lt;/strong&gt;.The  &lt;strong&gt;makeAdder(6)&lt;/strong&gt; has a thing in mind that it will return a function(y) and stores the value of x in its memory that's why value of x is used by the function(y) instead of context execution of makeAdder().And we have both values of x and y in our output   &lt;/p&gt;

&lt;p&gt;In short, closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
