<?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: Mihlali Jordan</title>
    <description>The latest articles on DEV Community by Mihlali Jordan (@mihlalijordan).</description>
    <link>https://dev.to/mihlalijordan</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%2F457970%2F55a350a5-484a-46a2-a6fb-ead967223d02.jpeg</url>
      <title>DEV Community: Mihlali Jordan</title>
      <link>https://dev.to/mihlalijordan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mihlalijordan"/>
    <language>en</language>
    <item>
      <title>JavaScript Closures</title>
      <dc:creator>Mihlali Jordan</dc:creator>
      <pubDate>Fri, 08 Jan 2021 15:49:04 +0000</pubDate>
      <link>https://dev.to/mihlalijordan/javascript-closures-d8l</link>
      <guid>https://dev.to/mihlalijordan/javascript-closures-d8l</guid>
      <description>&lt;p&gt;In today's blog post, I am going to attempt to explain a concept I have grown to love and appreciate, one that is also commonly misunderstood and somewhat complex: JavaScript Closures. &lt;/p&gt;

&lt;p&gt;Before we go any further, there is a term that I am going to use excessively throughout this article which is very important to understand. That term is &lt;strong&gt;execution context&lt;/strong&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We can think of &lt;strong&gt;execution context&lt;/strong&gt; as the environment the current code is being evaluated in. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So for example &lt;strong&gt;global execution context&lt;/strong&gt; would refer to the global environment – the default environment where your code first runs whenever you execute a program. &lt;strong&gt;Local execution context&lt;/strong&gt; would refer to the local context of a function. Variables declared in the global execution context are referred to as &lt;strong&gt;global variables&lt;/strong&gt; and variables declared within the local execution context of a given function are referred to as &lt;strong&gt;local variables&lt;/strong&gt; as in their are local to the context and scope of that particular function. &lt;/p&gt;

&lt;p&gt;The one last term I would like to explain before we jump into things is the &lt;strong&gt;Call Stack&lt;/strong&gt;. This is basically a mechanism JavaScript uses to keep track of where it is in the order of execution within a program. &lt;/p&gt;

&lt;h3&gt;
  
  
  What happens when we invoke a function?
&lt;/h3&gt;

&lt;p&gt;It is very important to understand what is going on under the hood every time we run a function. Let's briefly go over this: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A brand new execution context is created – a local execution context and inside of this context all local variables of that function will be stored. &lt;/li&gt;
&lt;li&gt;The local execution context of the function is added at the very top of the call stack.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What happens when the function terminates?
&lt;/h3&gt;

&lt;p&gt;A function is terminated when it encounters either a &lt;code&gt;return&lt;/code&gt; statement or a closing bracket &lt;code&gt;}&lt;/code&gt;. So what happens under the hood when a function encounters either one of these?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The local execution context of the function is removed from the call stack. &lt;/li&gt;
&lt;li&gt;If the function has any return values, it will return them out into the context within which it was called. If the function was called in the global context, the value will be returned out to the global context. If the function was called within the local context of another function, the value will be returned out to the local context of the function within which the function returning out the value was called. If the function does not have any return values, it returns out &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Lastly, the local execution context of that function is destroyed. This means that it no longer exists in memory and all the variables that were declared within that local execution context are destroyed and removed from memory as well. They are no longer available. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What does this look like in practice?
&lt;/h3&gt;

&lt;p&gt;Let's take a look at the function below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let x = 5
function squared(a) {
  let squaredValue = a * a
  return squaredValue
}
let y = squared(x)
console.log(y)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this looks pretty simple, but there is quite a lot going on here. Let's go through this line by line. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Line 1 – First we declare a variable &lt;code&gt;x&lt;/code&gt; in the global execution context and assign it the value 5. This variable is stored in global memory. &lt;/li&gt;
&lt;li&gt;Still in the global execution context, we then declare the variable &lt;code&gt;squared&lt;/code&gt; and assign to it a function definition. So what has happened here is that the code inside the function block is stored in memory inside the variable &lt;code&gt;squared&lt;/code&gt;. At this point in time, the function is not invoked and the code inside the function block does not run – it is only stored in global memory. &lt;/li&gt;
&lt;li&gt;Line 7 – We then jump down to right after the function declaration and declare the variable &lt;code&gt;y&lt;/code&gt; and assign to it the result of the invocation of the function &lt;code&gt;squared&lt;/code&gt; with the argument &lt;code&gt;x&lt;/code&gt; passed into it. Before &lt;code&gt;squared&lt;/code&gt; is invoked, the variable &lt;code&gt;x&lt;/code&gt; is uninitialised. &lt;/li&gt;
&lt;li&gt;The function &lt;code&gt;squared&lt;/code&gt; is invoked. &lt;/li&gt;
&lt;li&gt;A brand new local execution context is created – we shall call this "squared execution context". &lt;/li&gt;
&lt;li&gt;We head back up to the function declaration and into the function block. 
&lt;strong&gt;NOTE:&lt;/strong&gt; Javascript does not go back up to the code. The thread of execution is top down, it never goes up and in this case does not jump from line 6 back up to line 2 – it does not have to because when it saved &lt;code&gt;squared&lt;/code&gt; in the beginning of the code, it saved all of the code in memory which is where it grabs the code from to start executing it.&lt;/li&gt;
&lt;li&gt;We head into the function block in lines 2 and 3 – now you might be tempted to say that the first thing that happens is that we declare the variable &lt;code&gt;squaredValue&lt;/code&gt; and assign to it the result of the expression &lt;code&gt;a * a&lt;/code&gt; but there is a step before that. Before this, JavaScript looks for any arguments passed in. Because this function accepts arguments, and has the parameter &lt;code&gt;a&lt;/code&gt;, a new variable &lt;code&gt;a&lt;/code&gt; is declared in the local execution context and assigned to it is the argument that was passed in when the function was invoked which is &lt;code&gt;x&lt;/code&gt;. JavaScript will look in global memory for &lt;code&gt;x&lt;/code&gt; and find that the value is assigned to it is 5 and then assign the value 5 to &lt;code&gt;a&lt;/code&gt;. Note that &lt;code&gt;a&lt;/code&gt; is stored in the memory of the "squared execution context". &lt;/li&gt;
&lt;li&gt;The value is returned out to global context and is stored in variable &lt;code&gt;y&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;The "squared execution context" is removed from the call stack and all the variables that were defined in the "squared execution context" are erased as well. &lt;/li&gt;
&lt;li&gt;Line 7 – We then log the value of &lt;code&gt;y&lt;/code&gt; out onto the console. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  JavaScript: A lexically scoped language
&lt;/h3&gt;

&lt;p&gt;We're getting there. I know this might seem like a lot but all of this is crucial to understanding closures. The next concept I want to touch on is &lt;strong&gt;Lexical Scope&lt;/strong&gt;. Well firstly, what is scope 🤔? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Scope&lt;/strong&gt; is essentially a set of rules that define what data is available to you at any given line in the running of your code. &lt;br&gt;
JavaScript uses lexical (static) scoping. Let's take a function for example; wherever that function is saved determines what data the function will have access to whenever it runs. Let's take a quick detour and look at this quick example.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let num = 3
function foo(x) {
  return x + num
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where is &lt;code&gt;foo&lt;/code&gt; declared? In global context and as such has access to all the variables in the global context.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function foo(){
  let num2 = 10
  return num2 * num2
}

function doSomething(){
  return num2 + 10
}

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

&lt;/div&gt;



&lt;p&gt;What do you think will happen when we try run the last line of this code? We will get a reference error of "&lt;code&gt;num2&lt;/code&gt; is not defined". We get this error because &lt;code&gt;num2&lt;/code&gt; is out of scope and not within the scope of &lt;code&gt;doSomething&lt;/code&gt;. JavaScript first looks in the function's local context for a variable and if it's not there, looks further up in its calling context and it if the variable is not found there still will continue further up until it reaches the global execution context. If the variable is not found in the global execution context, JavaScript will assign the value &lt;code&gt;undefined&lt;/code&gt; to the variable. &lt;/p&gt;

&lt;p&gt;In our example above, JavaScript looks inside of the local execution context of &lt;code&gt;doSomething&lt;/code&gt; for the variable &lt;code&gt;num2&lt;/code&gt; which it does not find and so it jumps up to the global context and still does not find the variable there and so it assigns the value &lt;code&gt;undefined&lt;/code&gt; to it. &lt;/p&gt;

&lt;p&gt;So &lt;strong&gt;lexical scope&lt;/strong&gt; just means that a function has access to variables that are defined in its calling context. Where the function is defined determines what variables it has access to.&lt;/p&gt;

&lt;p&gt;Now there are quite a number of caveats when it comes to scope which are beyond the scope of this article (do you get it? 😉😂). Maybe in future, I will post another article that deals specifically with scope in JavaScript. &lt;/p&gt;

&lt;h3&gt;
  
  
  A function returning another function? 💭
&lt;/h3&gt;

&lt;p&gt;We are almost there! This is the final piece of the puzzle before we get to the beauty that are closures. &lt;/p&gt;

&lt;p&gt;So, in JavaScript, it is very possible for us to return functions from other functions. Let's take a look at an 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 firstFunction(){
  function secondFunction(x) {
    return x * x
  }
  return secondFunction
}
const createdFunction = firstFunction()
const finalResult = createdFunction(10)
console.log(finalResult)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's dive deeper into what exactly is going on in the above code. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In line 1 we have our first function declaration. We declare the variable &lt;code&gt;firstFunction&lt;/code&gt; and we assign a function definition to it which is essentially the body of the function.&lt;/li&gt;
&lt;li&gt;We head down to line 7 where we declare the variable &lt;code&gt;createdFunction&lt;/code&gt; and assign to it the result of the invocation of &lt;code&gt;firstFunction&lt;/code&gt;. Until &lt;code&gt;firstFunction&lt;/code&gt; is invoked, &lt;code&gt;createdFunction&lt;/code&gt; is uninitialised. &lt;/li&gt;
&lt;li&gt;We invoke &lt;code&gt;firstFunction&lt;/code&gt; and create a brand new local execution context which we will refer to as &lt;em&gt;first local execution context&lt;/em&gt;. When this invocation takes place, &lt;em&gt;first local execution context&lt;/em&gt; is added to the top of the call stack.&lt;/li&gt;
&lt;li&gt;We are now back at line 2, inside the &lt;em&gt;first local execution context&lt;/em&gt; where we declare the variable &lt;code&gt;secondFunction&lt;/code&gt; and assign to it a function definition. This variable is stored inside the memory of &lt;em&gt;first local execution context&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;In line 5, we return out the value of &lt;code&gt;secondFunction&lt;/code&gt; to the global execution context where it is stored inside the variable &lt;code&gt;createdFunction&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;first local execution context&lt;/em&gt; is removed from the call stack and it is destroyed and so are all of the variables that were defined in and stored in the memory of the &lt;em&gt;first local execution context&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;In the final line, line 8, we declare the variable &lt;code&gt;finalResult&lt;/code&gt; in the global execution context and assign to it the result of the invocation of &lt;code&gt;createdFunction(10)&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;We invoke &lt;code&gt;createdFunction&lt;/code&gt; with the value 10 passed in as an argument. &lt;/li&gt;
&lt;li&gt;Upon invocation, a brand new local execution context is created, we will call this &lt;em&gt;second local execution context&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Second local execution context&lt;/em&gt; is added onto the top of the call stack.&lt;/li&gt;
&lt;li&gt;The first thing JavaScript will do once the function is invoked, before even heading to the function body is check whether the function has parameters and whether arguments were passed in so the values of the respective arguments can be assigned to the parameters. With our example, there is a parameter: &lt;code&gt;x&lt;/code&gt;. JavaScript will declare a variable &lt;code&gt;x&lt;/code&gt; in the &lt;em&gt;second local execution context&lt;/em&gt; and assign to it the value that was passed in as an argument which in this case is 10. So right now, in the memory of &lt;em&gt;second local execution context&lt;/em&gt; is variable &lt;code&gt;x&lt;/code&gt; with the value 10.&lt;/li&gt;
&lt;li&gt;In line 3 of the code the result of the expression &lt;code&gt;x * x&lt;/code&gt; is returned out. Where is this value returned out to? You might think that it is returned out to &lt;em&gt;first local execution context&lt;/em&gt;. It might look like that because the &lt;code&gt;secondFunction&lt;/code&gt; is nested inside of &lt;code&gt;firstFunction&lt;/code&gt; but that is not actually the case. Remember that at this point &lt;em&gt;first local execution context&lt;/em&gt; does not exist and &lt;code&gt;createdFunction&lt;/code&gt; is not being called inside of &lt;code&gt;firstFunction&lt;/code&gt;. &lt;strong&gt;This would be a good time to remind you that a function will return a value out into its calling context&lt;/strong&gt;. &lt;code&gt;createdFunction&lt;/code&gt; was called in global context and as such the result of the earlier expression (&lt;code&gt;x * x&lt;/code&gt;) will be returned out into the global execution context and stored in the variable &lt;code&gt;finalResult&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Once the value is returned out, &lt;em&gt;second local execution context&lt;/em&gt; is popped off the call stack and is erased along with all of the variables that were defined inside of it. &lt;/li&gt;
&lt;li&gt;We then finally log to the console the value of &lt;code&gt;finalResult&lt;/code&gt; is 100.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now you might be wondering why I went through all of these steps in so much detail. The primary reason is to place emphasis on understanding what is going on in each line of code. The second reason is so that you have it engrained in your understanding that a &lt;strong&gt;brand new&lt;/strong&gt; execution context is created every single time a function runs and that the newly created execution context is destroyed and erased when the function terminates along with all of the variables that were defined within that context. Lastly, I wanted to show you that functions can return functions – this is the fundamental pillar of the concept of closures.&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript Closures
&lt;/h3&gt;

&lt;p&gt;You made it! You're finally here. Now that we have laid down a solid foundation we can dig into what closures are and why they are such a wonderful feature in JavaScript. &lt;/p&gt;

&lt;p&gt;Before we look at an example I want to quickly provide a brief explanation of what a closure is. You can almost think of it as a function's own little piece of memory. Closures are essentially a function's way of keeping memory of all the data that was inside the surrounding context within which the function was initially defined. For those looking for a technical definition: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;closure&lt;/strong&gt; is a collection of all the variables that were in scope at the time of the function's creation. &lt;br&gt;
The closure contains all of the variables that are in scope at the time the function is created. &lt;strong&gt;Think of closure as a backpack that the function carries with it wherever it goes&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This might not make sense now so let's take a look at an 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 doSomething(){
  let num = 1
  function doubleNum(){
    num++
    console.log(num)
  }
  return doubleNum
}
const doSomethingElse = doSomething()
doSomethingElse()
doSomethingElse()
const doSomethingBetter = doSomething()
doSomethingBetter()
doSomethingBetter()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's dive into this function line by line. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In line 1 we declare the variable &lt;code&gt;doSomething&lt;/code&gt; and assign to it a function definition inside of the global execution context.&lt;/li&gt;
&lt;li&gt;We jump down to line 9 and declare the variable &lt;code&gt;doSomethingElse&lt;/code&gt; and assign to it the result of the invocation of &lt;code&gt;doSomething&lt;/code&gt;. Until &lt;code&gt;doSomething&lt;/code&gt; is invoked, &lt;code&gt;doSomethingElse&lt;/code&gt; is uninitialised. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;doSomething&lt;/code&gt; is invoked and we create a brand new local execution context which we will refer to as &lt;em&gt;doSomething execution context&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;doSomething execution context&lt;/em&gt; is added to the top of the call stack. &lt;/li&gt;
&lt;li&gt;We jump to line 2 inside the body of &lt;code&gt;doSomething&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;We declare the variable &lt;code&gt;num&lt;/code&gt; and assign to it the value 1. This variable is stored inside the memory of &lt;em&gt;doSomething execution context&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;In line 3, still inside &lt;em&gt;doSomething execution context&lt;/em&gt; we declare the variable &lt;code&gt;doubleNum&lt;/code&gt; and assign to it a function definition. This is where it gets exciting!! This is where we see the beauty of closures. When &lt;code&gt;doubleNum&lt;/code&gt; is defined, what also happens is that JavaScript creates a bind to the local variable environment through a hidden scope property on the function &lt;code&gt;doubleNum&lt;/code&gt;. This means that when we return out &lt;code&gt;doubleNum&lt;/code&gt;, attached to it is all of the local data. All the variables in scope at the time &lt;code&gt;doubleNum&lt;/code&gt; is defined are attached to &lt;code&gt;doubleNum&lt;/code&gt;. &lt;code&gt;doubleNum&lt;/code&gt; has &lt;strong&gt;closure&lt;/strong&gt; which contains inside of it all the variables that are in the context of which it is defined. So in the case of the above code, inside the closure of &lt;code&gt;doubleNum&lt;/code&gt; are all of the variables inside of &lt;em&gt;doSomething execution context&lt;/em&gt; which is basically just &lt;code&gt;num&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;In line 7, we return out &lt;code&gt;doubleNum&lt;/code&gt;. Not only do we return out the function definition, we also return out the closure – all of the variables in &lt;em&gt;doSomething execution context&lt;/em&gt;. So the function definition assigned to &lt;code&gt;doubleNum&lt;/code&gt; and its closure is returned out to the global context and stored in the variable &lt;code&gt;doSomethingElse&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;After we return out &lt;code&gt;doubleNum&lt;/code&gt;, the &lt;em&gt;doSomething execution context&lt;/em&gt; is removed from the call stack and destroyed along with &lt;em&gt;all&lt;/em&gt; of the variables that were defined inside of &lt;em&gt;doSomething execution context&lt;/em&gt;. To further emphasise, at this point, &lt;code&gt;doubleNum&lt;/code&gt; and &lt;code&gt;num&lt;/code&gt; are erased as the &lt;em&gt;doSomething execution context&lt;/em&gt; no longer exists. &lt;/li&gt;
&lt;li&gt;We are now at line 10 and we invoke &lt;code&gt;doSomethingElse&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;A brand new execution context is created which we will call the &lt;em&gt;doSomethingElse execution context&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;doSomethingElse execution context&lt;/em&gt; is added onto the call stack. &lt;/li&gt;
&lt;li&gt;We are now at line 4 inside the function body of &lt;code&gt;doubleNum&lt;/code&gt;. &lt;em&gt;Remember, we are only jumping back up there so we can see what code is being executed when &lt;code&gt;doSomethingElse&lt;/code&gt; is invoked. JavaScript has the function definition stored in memory and does not need to go back up in the execution thread&lt;/em&gt;. In line 4, we increment &lt;code&gt;num&lt;/code&gt; by 1. Before JavaScript performs the increment operation, it needs to get a value for &lt;code&gt;num&lt;/code&gt;. So where will it look first? You guessed it – in the closure. Stored in the closure attached to the function definition that was assigned to &lt;code&gt;doSomethingElse&lt;/code&gt;, is the variable &lt;code&gt;num&lt;/code&gt; which has a value of 1. JavaScript then increments &lt;code&gt;num&lt;/code&gt; by 1. This value is updated within the closure of &lt;code&gt;doSomethingElse&lt;/code&gt;. This means that now &lt;code&gt;num&lt;/code&gt; is 2. &lt;/li&gt;
&lt;li&gt;We then console log out the value which is 2. &lt;/li&gt;
&lt;li&gt;Back at line 11, we invoke &lt;code&gt;doSomethingElse&lt;/code&gt; again and repeat steps &lt;code&gt;11&lt;/code&gt; - &lt;code&gt;14&lt;/code&gt;. The are two differences this time however. The first difference is that we are no longer dealing with &lt;em&gt;doSomethingElse execution context&lt;/em&gt;, we are dealing with an entirely &lt;strong&gt;brand new&lt;/strong&gt; execution context which we can refer to as &lt;em&gt;doSomethingElse2 execution context&lt;/em&gt;. The second difference is that in step 13 when JavaScript looks to increment &lt;code&gt;num&lt;/code&gt;, it looks inside the closure and finds that the value is 2 (as a result of the first invocation of &lt;code&gt;doSomethingElse&lt;/code&gt;) and the result of the operation will be 3. &lt;/li&gt;
&lt;li&gt;With &lt;em&gt;doSomethingElse2 execution context&lt;/em&gt; removed from the call stack, we are now at line 12 where we declare the variable &lt;code&gt;doSomethingBetter&lt;/code&gt; and assign to it the result of the invocation of &lt;code&gt;doSomething&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;We then repeat steps &lt;code&gt;3&lt;/code&gt;-&lt;code&gt;9&lt;/code&gt; with two key differences. The first being that again, a &lt;strong&gt;brand new&lt;/strong&gt; execution context is created and we can call this &lt;em&gt;doSomething2 execution context&lt;/em&gt;. &lt;em&gt;doSomething execution context&lt;/em&gt; no longer exists. The second key difference to note is that a &lt;strong&gt;brand new&lt;/strong&gt; closure is created and returned out to the variable &lt;code&gt;doSomethingBetter&lt;/code&gt; along with the function definition. &lt;strong&gt;Remember this as it will prove important in the next couple of steps – &lt;code&gt;doSomethingElse&lt;/code&gt; and &lt;code&gt;doSomethingBetter&lt;/code&gt; have different closures&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Now at line 13, we invoke the function &lt;code&gt;doSomethingBetter&lt;/code&gt;. We repeat steps &lt;code&gt;11&lt;/code&gt; - &lt;code&gt;14&lt;/code&gt; with two important differences to remember. The first being that a &lt;strong&gt;brand new&lt;/strong&gt; execution context is created which we will call &lt;em&gt;doSomethingBetter execution context&lt;/em&gt;. The second important thing to remember is the value of &lt;code&gt;num&lt;/code&gt;. You might be tempted to say that when JavaScript looks inside the closure for the value of &lt;code&gt;num&lt;/code&gt; it will be 3 because of the previous invocations of &lt;code&gt;doSomethingElse&lt;/code&gt;. This is not the case. Because &lt;code&gt;doSomethingElse&lt;/code&gt; and &lt;code&gt;doSomethingBetter&lt;/code&gt; have different closures, the value of &lt;code&gt;num&lt;/code&gt; initially in the closure of &lt;code&gt;doSomethingBetter&lt;/code&gt; is 1 as this was the value of &lt;code&gt;num&lt;/code&gt; when the function definition was returned out to &lt;code&gt;doSomethingBetter&lt;/code&gt;. Having said that, the value of &lt;code&gt;num&lt;/code&gt; inside the closure of &lt;code&gt;doSomethingBetter&lt;/code&gt; will be incremented by 1 resulting in 3. &lt;/li&gt;
&lt;li&gt;Once the operation has taken place and &lt;em&gt;doSomethingBetter execution context&lt;/em&gt; is removed from the call stack and destroyed, we go to the final line, line 14 where we invoke &lt;code&gt;doSomethingBetter&lt;/code&gt; a second time. &lt;/li&gt;
&lt;li&gt;A brand new execution context is created we run through steps &lt;code&gt;11&lt;/code&gt; - &lt;code&gt;14&lt;/code&gt; again taking into account the fact that the value of &lt;code&gt;num&lt;/code&gt; inside the closure of &lt;code&gt;doSomethingBetter&lt;/code&gt; is now 2 and that we are dealing with a brand new execution context and that &lt;em&gt;doSomethingBetter execution context&lt;/em&gt; does not exist at this stage. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now this might seem like a mouthful and quite a lot to digest but there are a few things I wanted to highlight in this illustration. The first is that every time a function is returned out and stored in a variable, the function definition of that function has attached to it its own unique closure. We see this with &lt;code&gt;doSomethingElse&lt;/code&gt; and &lt;code&gt;doSomethingBetter&lt;/code&gt; – the variable &lt;code&gt;num&lt;/code&gt; in the closures of these functions are not the same. &lt;strong&gt;&lt;em&gt;Each individual function gets its own private closure with data inside from the running of the function &lt;code&gt;doSomething&lt;/code&gt;, each has their own compartmentalised pocket of data&lt;/em&gt;&lt;/strong&gt;.  The second point I wanted to drive home is that a new execution context is created &lt;strong&gt;every single&lt;/strong&gt; time a function is invoked and that execution context is &lt;strong&gt;destroyed&lt;/strong&gt; when the function is terminated. &lt;/p&gt;

&lt;h3&gt;
  
  
  What's the point?
&lt;/h3&gt;

&lt;p&gt;At this stage you might still be wondering why and how this could be remotely useful? Well the beauty with closures is that they give our functions persistent memories and an entirely new toolkit for writing professional code. They give us the toolset to create helper functions like &lt;code&gt;once&lt;/code&gt; and &lt;code&gt;memoize&lt;/code&gt;. Iterators and generators use lexical scoping and closure to achieve patterns for handling data in JS. Closures help us in Asynchronous JavaScript – callbacks and promises rely on closure to persist state in an asynchronous environment. &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;If this all gets to complex for you, think of closure as a backpack that a given function carries around with it everywhere it goes. When it gets returned out of another function it gets returned out carrying its backpack which has inside of it all of the variables that were in the local environment where the function would have been defined. &lt;/p&gt;

&lt;p&gt;I would like to give credit and a shoutout to &lt;a href="http://willsentance.com/"&gt;Will Sentance&lt;/a&gt; for explaining this concept so wonderfully in the JS Hard Parts Series. &lt;/p&gt;

&lt;p&gt;If you enjoyed reading this and you found it useful, please do not forget to share! 🚀 💥&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>closure</category>
    </item>
    <item>
      <title>Redux in less than five minutes</title>
      <dc:creator>Mihlali Jordan</dc:creator>
      <pubDate>Wed, 02 Sep 2020 04:01:41 +0000</pubDate>
      <link>https://dev.to/mihlalijordan/redux-in-less-than-five-minutes-3heg</link>
      <guid>https://dev.to/mihlalijordan/redux-in-less-than-five-minutes-3heg</guid>
      <description>&lt;p&gt;Heelllooo there. Now if you were anything like me, you probably found learning Redux quite a cumbersome task. The purpose of this article is to simplify the concepts and bring you to a high level understanding of Redux and how it works. &lt;/p&gt;

&lt;h3&gt;
  
  
  Store
&lt;/h3&gt;

&lt;p&gt;Redux in a nutshell is a state management library. One of the core parts of Redux is what is called the &lt;code&gt;store&lt;/code&gt; which you could think of as a cloud that sits on top of your application and holds the current state of your app at a given point in time. &lt;/p&gt;

&lt;p&gt;So, a few things you need to know about the store : &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is the single source of truth, meaning all your UI elements fetch state and any updates to application state from the store and only from the store&lt;/li&gt;
&lt;li&gt;The store is not directly mutated because the store in its nature is an immutable object. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So how then do we actually update the state? I am glad you asked. Because the store is an immutable object, we write a function called a reducer, that takes the store as an argument and returns an updated store with the updated state. &lt;/p&gt;

&lt;h3&gt;
  
  
  Reducer
&lt;/h3&gt;

&lt;p&gt;Another core piece of Redux is what is called the &lt;code&gt;reducer&lt;/code&gt;. All the reducer really is, is a function that takes the current instance of the store and returns an updated store. The reducer does not touch global state at all, mutate any arguments or have any side effects. &lt;/p&gt;

&lt;h3&gt;
  
  
  Action
&lt;/h3&gt;

&lt;p&gt;The last major piece in this puzzle is called the &lt;code&gt;action&lt;/code&gt;. The action is just a plain JavaScript object that describes what just happened. The action represents what just happened and can really represent any action a user might perform on your application e.g. adding an item to cart, deleting a record, updating their name on their profile etc. The action informs the reducer on what needs to be changed. Based on the type of action, the reducer then knows what properties of the state must be updated. &lt;/p&gt;

&lt;h3&gt;
  
  
  How does it all work under the hood?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Whenever an action is performed by the user, an action object is created and dispatched. This is done through the dispatch method which takes in an action as an argument&lt;/li&gt;
&lt;li&gt;The store will forward the action to the reducer
(Note that the store is in charge of calling the reducer)&lt;/li&gt;
&lt;li&gt;The reducer will then receive the action, compute a new state based on the action type and will then return the new state to the store &lt;/li&gt;
&lt;li&gt;The store will then set the state internally and notify all the UI components about the update&lt;/li&gt;
&lt;li&gt;The UI components will then pull the updated state from the store and refresh themselves accordingly. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is a VERY, VERY high-level explanation of Redux. Under the hood, it is a bit more complex but it is important to understand the basics because it will inform your understanding of the deeper operations and complexities of the library. I hope this helped! Please share if you think this might help someone else! &lt;/p&gt;

&lt;p&gt;Till next time 😃&lt;/p&gt;

</description>
      <category>redux</category>
      <category>react</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
