<?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: Yamin</title>
    <description>The latest articles on DEV Community by Yamin (@yaminmhd).</description>
    <link>https://dev.to/yaminmhd</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%2F64227%2F668a4159-f7f0-4d69-9992-5279e35d4f14.png</url>
      <title>DEV Community: Yamin</title>
      <link>https://dev.to/yaminmhd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yaminmhd"/>
    <language>en</language>
    <item>
      <title>Understanding Javascript Closures</title>
      <dc:creator>Yamin</dc:creator>
      <pubDate>Wed, 12 Dec 2018 14:01:06 +0000</pubDate>
      <link>https://dev.to/yaminmhd/understanding-javascript-closures-1fk7</link>
      <guid>https://dev.to/yaminmhd/understanding-javascript-closures-1fk7</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnz2s77281zecs8fnu9in.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnz2s77281zecs8fnu9in.jpeg" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was really a hard concept to understand and it took me a while to comprehend what closures are. So here is my understanding of what it is with diagrams and code snippets for better visualisation&lt;/p&gt;

&lt;p&gt;To warm up my brain, I will start with an example that doesn't involve closures and slowly change my example to involve closures at the end&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add1(){
  var x = 1;
  var f = function(y){
    return x + y;
  }
  return f(3);
}

console.log(add1());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have a simple function called &lt;code&gt;add1&lt;/code&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It has a local variable &lt;code&gt;x&lt;/code&gt; which has a value of &lt;code&gt;1&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Another variable &lt;code&gt;f&lt;/code&gt; which is assigned a function&lt;/li&gt;
&lt;li&gt;This &lt;code&gt;add1&lt;/code&gt; function returns the function &lt;code&gt;f&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So if we run this code with it will return &lt;code&gt;4&lt;/code&gt; with &lt;code&gt;x&lt;/code&gt; = 1 and &lt;code&gt;y&lt;/code&gt; = 3&lt;/p&gt;

&lt;p&gt;Since we have a function declaration and that function is called in the console.log. The execution context will be as follows: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4t547f2ef0i1s1rz72e6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4t547f2ef0i1s1rz72e6.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So first of all, we will have the &lt;code&gt;global execution context&lt;/code&gt; being created. We will have the &lt;code&gt;creation&lt;/code&gt; and &lt;code&gt;execution&lt;/code&gt; phase. In the &lt;code&gt;creation&lt;/code&gt; phase, we will hoist the &lt;code&gt;add1&lt;/code&gt; function to the top of the scope and in the &lt;code&gt;execution&lt;/code&gt; phase we will execute the &lt;code&gt;add1&lt;/code&gt; function&lt;/p&gt;

&lt;p&gt;Calling the &lt;code&gt;add1&lt;/code&gt; function will create &lt;code&gt;add1&lt;/code&gt; execution context and it will be as follows:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjjseosysl1f21lcx0yf5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjjseosysl1f21lcx0yf5.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this execution context, we will hoist the variables &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;f&lt;/code&gt; in the &lt;code&gt;creation&lt;/code&gt; phase and both are undefined in the &lt;code&gt;creation&lt;/code&gt; phase. In the &lt;code&gt;execution&lt;/code&gt; phase, &lt;code&gt;x&lt;/code&gt; is assigned the value of &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;f&lt;/code&gt; is assigned to the function that accepts a argument &lt;code&gt;y&lt;/code&gt;. After assignment, it is then returning function call &lt;code&gt;f(3)&lt;/code&gt;;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F312s23b87nfi7v68nsba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F312s23b87nfi7v68nsba.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This creates another execution context which is called f. In &lt;code&gt;f&lt;/code&gt; execution context, we are not hoisting anything in the creation phase as there are no variables or function declared. In the execution phase, it is returning &lt;code&gt;x+y&lt;/code&gt; where the value of &lt;code&gt;x&lt;/code&gt; is retrieved from climbing the scope chain in the &lt;code&gt;add1&lt;/code&gt; execution context and the value of &lt;code&gt;y&lt;/code&gt; is passed in by &lt;code&gt;f(3)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So in overall &lt;code&gt;x&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;. Returning &lt;code&gt;4&lt;/code&gt;. After each EC is executed, it is then popped off the stack one by one. &lt;/p&gt;

&lt;p&gt;Let's take a look the scope chain&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzj3uhvufbk0zhln7g7vh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzj3uhvufbk0zhln7g7vh.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;f&lt;/code&gt; execution context, it is returning &lt;code&gt;x+y&lt;/code&gt;. The value of &lt;code&gt;y&lt;/code&gt; is readily available in this context being the value of &lt;code&gt;3&lt;/code&gt; but the value of &lt;code&gt;x&lt;/code&gt; is not. So it reference the outer execution context and retrieve the value of &lt;code&gt;x&lt;/code&gt; which is &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;



&lt;p&gt;Phew.. That was alot. Hopefully you are able to grasp the execution context and scoping. So now we are ready to move on to something a bit more complicated that is related to closures. Here is the code snippet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var add = function() {
  var x = 1;
  var f = function(y) {
    return x + y;
  };
  return f;
};

var g = add();
console.log(g(3));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example is different from the first one where we are now returning a reference to the function and not the value. Then we are calling the function outside the scope of the function &lt;code&gt;add&lt;/code&gt; when &lt;code&gt;g(3)&lt;/code&gt; is called. Probably this part might be hard to grasp.&lt;/p&gt;

&lt;p&gt;So let's go through the execution context once more and see how this adds up. First the global execution context&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fstkt5plsu7dhrka35l3v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fstkt5plsu7dhrka35l3v.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Followed by the &lt;code&gt;add&lt;/code&gt; execution context&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fiwkgp70xlmq323nupl3m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fiwkgp70xlmq323nupl3m.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now comes the interesting part. Once the &lt;code&gt;add&lt;/code&gt; execution context finish executing, it will be popped off the stack &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fstkt5plsu7dhrka35l3v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fstkt5plsu7dhrka35l3v.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next will be the g(3) execution context. Remember that the variable &lt;code&gt;g&lt;/code&gt; contains the function &lt;code&gt;f&lt;/code&gt; since that is the function that was returned in &lt;code&gt;add&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F9nn04jp2s8mjpso394cv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F9nn04jp2s8mjpso394cv.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;g&lt;/code&gt; will return &lt;code&gt;x + y&lt;/code&gt;.We are running &lt;code&gt;g(3)&lt;/code&gt; so value of &lt;code&gt;y&lt;/code&gt; will be 3.&lt;br&gt;
What about the value of &lt;code&gt;x&lt;/code&gt;? &lt;/p&gt;

&lt;p&gt;Well if we look around, we won't be able to find &lt;code&gt;x&lt;/code&gt;. But we remember that &lt;code&gt;x&lt;/code&gt; was set to &lt;code&gt;1&lt;/code&gt; in another execution context that was created, executed and popped off.&lt;/p&gt;

&lt;p&gt;So in &lt;code&gt;Javascript&lt;/code&gt;, if a function is created inside another function like &lt;code&gt;g&lt;/code&gt;. So &lt;code&gt;g&lt;/code&gt; will keep a reference to variables that were in the scope of the enclosing function in our case &lt;code&gt;add&lt;/code&gt;. So meaning &lt;code&gt;g&lt;/code&gt; still has got access to the memory of execution context of &lt;code&gt;add&lt;/code&gt; and that includes the variable &lt;code&gt;x&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjrojqnwwdsp38od8gjoi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjrojqnwwdsp38od8gjoi.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, the &lt;code&gt;add&lt;/code&gt; execution context has ended, but &lt;code&gt;g&lt;/code&gt; is still allowed to access the variables of the execution context which are still in the memory of the execution context of &lt;code&gt;add&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our function &lt;code&gt;g&lt;/code&gt; can still climb up the scope chain and find &lt;code&gt;x&lt;/code&gt; although it's not in the execution stack anymore.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;g's&lt;/code&gt; execution context has &lt;code&gt;closed in&lt;/code&gt; &lt;code&gt;x&lt;/code&gt; which is an outer variable even if the &lt;code&gt;x's&lt;/code&gt; execution context is gone. So our function &lt;code&gt;g&lt;/code&gt; is a &lt;code&gt;closure&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Closures like the &lt;code&gt;g&lt;/code&gt; function internally store references to outer variables.&lt;/p&gt;

&lt;p&gt;Let's take a look at the scope chain for this&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjgk1tbm2jcls74ekuqlc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjgk1tbm2jcls74ekuqlc.png" title=" Execution context" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have the global execution context scope. We will have the &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;g&lt;/code&gt; variable and their execution context.&lt;/p&gt;

&lt;p&gt;After line &lt;code&gt;var g = add();&lt;/code&gt; finishes, the add execution context gets popped off the stack.(It's shaded for this reason)&lt;/p&gt;

&lt;p&gt;We then create and execute the &lt;code&gt;g&lt;/code&gt; execution context. Where it will still be able to reference the &lt;code&gt;x&lt;/code&gt; outer variable even after the &lt;code&gt;x's&lt;/code&gt; execution context is not on the stack anymore&lt;/p&gt;




&lt;p&gt;Hopefully you were able to get some idea on what a closure is from this article. If you're still confused, please do read the following reference.&lt;/p&gt;

&lt;p&gt;Here is the main reference that I used to further my understanding on this topic&lt;br&gt;
&lt;a href="https://tylermcginnis.com/ultimate-guide-to-execution-contexts-hoisting-scopes-and-closures-in-javascript/" rel="noopener noreferrer"&gt;Ultimate guide to execution context&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>closures</category>
    </item>
  </channel>
</rss>
