<?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: muhd</title>
    <description>The latest articles on DEV Community by muhd (@muhd).</description>
    <link>https://dev.to/muhd</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%2F1152285%2Fb46290d6-68c6-43b5-894a-d99219cda399.png</url>
      <title>DEV Community: muhd</title>
      <link>https://dev.to/muhd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muhd"/>
    <language>en</language>
    <item>
      <title>JavaScript Closures: Demystified</title>
      <dc:creator>muhd</dc:creator>
      <pubDate>Sat, 23 Dec 2023 00:13:53 +0000</pubDate>
      <link>https://dev.to/muhd/javascript-closures-demystified-12dn</link>
      <guid>https://dev.to/muhd/javascript-closures-demystified-12dn</guid>
      <description>&lt;p&gt;In the smoky, dimly lit room of JavaScript, a concept lurks in the shadows, waiting to be unveiled. We call it a "&lt;strong&gt;closure.&lt;/strong&gt;" Now, hang on to your hats because we're about to embark on a journey into the heart of this enigmatic creature.&lt;/p&gt;

&lt;p&gt;When functions can be treated as values and local bindings are recreated upon each function call, an intriguing question arises: When the function call is no longer active, what happens to the local bindings?&lt;/p&gt;

&lt;p&gt;For example;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;rememberValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;local&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;local&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wrap1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rememberValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wrap2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rememberValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;wrap1&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="c1"&gt;// → 1&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;wrap2&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="c1"&gt;// → 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code snippet above is permitted and works as expected - both instances of the binding remain accessible. This scenario effectively illustrates that local bindings are created afresh for each function call, and different calls do not interfere with each other's local bindings.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are local bindings?
&lt;/h2&gt;

&lt;p&gt;Let's start by defining bindings. In JavaScript, the term 'binding' is the formal language used to describe what many individuals commonly call a &lt;em&gt;variable.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;totalHoursSpentOnArticle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ford Arthur&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Bindings defined outside of any function or block have a scope that encompasses the entire program. As a result, you can reference such bindings from any part of your code. These bindings are commonly referred to as &lt;em&gt;"&lt;/em&gt;&lt;strong&gt;&lt;em&gt;global&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;"&lt;/em&gt; bindings.&lt;/p&gt;

&lt;p&gt;Bindings declared using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are, in fact, local to the block where they are declared.&lt;/p&gt;

&lt;p&gt;Therefore, if you create one of these bindings within a loop, the code segments located before and after the loop lack access to it. In the JavaScript version before 2015, scopes were only introduced by functions. Consequently, old-style bindings, created with the &lt;code&gt;var&lt;/code&gt; keyword, remained accessible throughout the function in which they were defined or at the global scope if not within a function.&lt;/p&gt;

&lt;p&gt;Bindings generated for function parameters or declared within a function have a scope limited to that specific function, earning them the name of &lt;strong&gt;&lt;em&gt;local bindings&lt;/em&gt;&lt;/strong&gt;. Upon each function call, fresh instances of these bindings are generated. This separation between functions offers a degree of isolation, where each function call operates within its unique context (local environment), often requiring minimal awareness of the broader global environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// → 60&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// → Uncaught ReferenceError: y is not defined&lt;/span&gt;
    &lt;span class="c1"&gt;// y is not visible here&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// → 40&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In the code snippet above, &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt; are the local bindings, while &lt;code&gt;x&lt;/code&gt; is a global binding.&lt;/p&gt;

&lt;p&gt;Every scope can &lt;em&gt;"look into"&lt;/em&gt; surrounding the scope, making &lt;code&gt;x&lt;/code&gt; visible inside the block within the provided example. However, an exception arises when multiple bindings share the same name  -  under such circumstances, the code can exclusively access the innermost binding with that name. For instance, when the code within the &lt;code&gt;square&lt;/code&gt; function references &lt;code&gt;n&lt;/code&gt;, it specifically refers to its &lt;code&gt;n&lt;/code&gt;, not the &lt;code&gt;n&lt;/code&gt; in the global scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="c1"&gt;// → 4&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// → 42&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This behavior, wherein a variable's scope is dictated by its position within the source code hierarchy, serves as an instance of &lt;strong&gt;&lt;em&gt;lexical scoping&lt;/em&gt;&lt;/strong&gt;&lt;strong&gt;.&lt;/strong&gt; Lexical scoping enables inner functions, like &lt;code&gt;square&lt;/code&gt; in this case, to access variables from their enclosing scopes based on their respective positions within the code's structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now, let's direct our gaze to the central element: Closures
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is a closure?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A closure combines a function enveloped with references to its surrounding context (the lexical environment). A lexical environment in programming refers to the environment in which code is written and executed.&lt;br&gt;
You might want to find out more about &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;lexical environments here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To put it differently, closure provides the means to reach the scope of an outer function from within an inner function.&lt;/p&gt;

&lt;p&gt;To use a closure, create a function within another function and make it accessible. You can render a function accessible by returning it or passing it to another function.&lt;br&gt;
Even after the outer function has been completed and returned, the inner function will retain access to the variables in the outer function's scope.&lt;br&gt;
Note the first part of the definition; &lt;em&gt;"&lt;/em&gt;&lt;strong&gt;&lt;em&gt;A closure is the combination of a function enveloped with references to its surrounding context&lt;/em&gt;&lt;/strong&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;"&lt;/em&gt; That's essentially a description of lexical scope!&lt;br&gt;
But we require including the second part of this definition to give an example of a closure. &lt;em&gt;"&lt;/em&gt;&lt;strong&gt;&lt;em&gt;Even after the outer function has been completed and returned, the inner function will retain access to the variables in the outer function's scope&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let us examine fascinating instances of closures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;theAnswer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;say&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="c1"&gt;// Local variable that ends up within the closure &lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;'Forty-two,' said Deep Thought&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;say&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;tellUs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;theAnswer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
    &lt;span class="nf"&gt;tellUs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ''Forty-two,' said Deep Thought'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have two functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An outer function &lt;code&gt;theAnswer&lt;/code&gt; has a variable &lt;code&gt;answer&lt;/code&gt; and returns the inner function &lt;code&gt;say&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;An inner function that returns the variable &lt;code&gt;answer&lt;/code&gt; when &lt;code&gt;tellUs&lt;/code&gt; is invoke.
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---eCRqahF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjd4fsocwwbzhstprssk.png" alt="Image man with red beams coming out of his head." width="800" height="448"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Duplicate the provided code example above and give it a go. What do you think?&lt;/p&gt;

&lt;p&gt;In the code example above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;theAnswer&lt;/code&gt; is called and creates an environment in which it defines a local variable answer and returns an inner function say.&lt;/li&gt;
&lt;li&gt;Next, we call the &lt;code&gt;theAnswer&lt;/code&gt; function and save the resulting inner function in the variable &lt;code&gt;tellUs&lt;/code&gt;. Logging the variable &lt;code&gt;tellUs&lt;/code&gt; returns something like what we have below:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
    &lt;span class="nx"&gt;ƒ&lt;/span&gt; &lt;span class="nf"&gt;theAnswer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;say&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
      &lt;span class="c1"&gt;// Local variable that ends up within the closure &lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;'Forty-two,' said Deep Thought&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;say&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;So that when we later call &lt;code&gt;tellUs&lt;/code&gt;, it proceeds to run the inner function say, which subsequently logs the value of &lt;code&gt;answer&lt;/code&gt; to the console, as shown below:&lt;/p&gt;

&lt;p&gt;'Forty-two,' said Deep Thought&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Getting your head around programs like these requires a bit of mental gymnastics. Picture function values as these packages carry both their inner workings and the surroundings they grew up in. When you evoke them, they bring that homey environment they were born in - no matter where you make the call from.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases of closures
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Data Privacy&lt;/strong&gt;&lt;br&gt;
Closures can be used to create private variables and functions. This ensures that certain data is inaccessible outside the scope, providing privacy and encapsulation.&lt;br&gt;
Here's an illustration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;      &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createCounter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Private variable enclosed by the closure&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;incrementCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createCounter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;incrementCounter&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;incrementCounter&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Allow me to offer a concise breakdown of its operation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;createCounter&lt;/code&gt; is a function that initializes by establishing a private variable named count with an initial value of 0 within its scope.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It returns an enclosed function, a closure, which, when called, increases the &lt;code&gt;count&lt;/code&gt; by 1 and returns the current value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;incrementCounter&lt;/code&gt; is assigned the result of invoking &lt;code&gt;createCounter&lt;/code&gt;, which means it holds the inner function (closure) that was returned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When &lt;code&gt;incrementCounter()&lt;/code&gt; is called, it triggers the execution of the inner function, increasing the &lt;code&gt;count&lt;/code&gt; and the return of the updated value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With every further use of &lt;code&gt;incrementCounter()&lt;/code&gt;, &lt;code&gt;count&lt;/code&gt; increments.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Callback functions&lt;/strong&gt;&lt;br&gt;
Closures are often used in dealing with asynchronous tasks and event-driven programming. They help create callback functions that remember and work with their surrounding environment, which is crucial when explaining event-based systems in documentation.&lt;br&gt;
Here's a code example demonstrating closures in callback functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Data from &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Closure captures 'data'&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Simulate a 1-second delay&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Processing: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://example.com/api/data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;processData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In our scenario, the &lt;code&gt;fetchData&lt;/code&gt; function simulates an asynchronous data retrieval process. It accepts both a URL and a callback function as its parameters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the &lt;code&gt;setTimeout&lt;/code&gt;, it generates some data and invokes the callback function, which captures the data variable from its surrounding scope due to closure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;processData&lt;/code&gt; function serves as our callback, responsible for handling and logging the data we receive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We call &lt;code&gt;fetchData&lt;/code&gt; with a URL and the &lt;code&gt;processData&lt;/code&gt; callback, demonstrating how closures empower the callback to reach and employ the &lt;code&gt;data&lt;/code&gt; variable from its surrounding scope.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Closures are a fundamental component of functional programming and may be found everywhere in JavaScript and other languages (not only functional ones).&lt;br&gt;
More use cases are;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Memoization&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Partial Applications&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt;&lt;strong&gt;urrying&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can check out more here on &lt;a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)#Applications"&gt;wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Clos(ure)ing remarks or thoughts?
&lt;/h2&gt;

&lt;p&gt;So, there you have it - the tale of JavaScript closures. They may seem cryptic initially, but once you've grasped their essence, you'll find they're a powerful tool in your programming arsenal. Use them wisely, and you'll unlock new realms of possibility in your technical journey. Happy coding, my friends.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Rd7pEbE7rjZz8vySuU/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Rd7pEbE7rjZz8vySuU/giphy.gif" alt="alt arnold gif" width="480" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;:&lt;/p&gt;
&lt;h2&gt;
  
  
  Okay real quick
&lt;/h2&gt;

&lt;p&gt;What would this return?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;MDN&lt;/a&gt; has a fantastic page on closures, as you might imagine, and&lt;/li&gt;
&lt;li&gt; The &lt;a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)#Applications"&gt;Wikipedia&lt;/a&gt; article on closure delves deeper into closure applications and how they work in various programming languages.&lt;/li&gt;
&lt;li&gt;You know what's fantastic? There's a treasure trove of valuable information waiting for you online! If you type "closures" into your Google search bar, you'll uncover an abundance of thrilling and fantastic insights on the topic. It's truly amazing and fascinating to dive into!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title># GitHub Education Pack: The North Star in Your Coding Sky</title>
      <dc:creator>muhd</dc:creator>
      <pubDate>Fri, 06 Oct 2023 23:37:38 +0000</pubDate>
      <link>https://dev.to/muhd/-github-education-pack-the-north-star-in-your-coding-sky-3pho</link>
      <guid>https://dev.to/muhd/-github-education-pack-the-north-star-in-your-coding-sky-3pho</guid>
      <description>&lt;p&gt;Are you a student, aspiring to be a developer? Are you frustrated by expensive tools? If so, this article is meant for you!&lt;/p&gt;

&lt;p&gt;Lots of tech-loving students face a tough challenge: they can't get the important tools and resources they need. Here's where &lt;strong&gt;GitHub Education&lt;/strong&gt; steps in. It's a platform that gives students real-world experience by providing free access to a range of developer tools. So, what's in it for you, for free through GitHub Education?&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the GitHub Education Pack?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mTrlvofu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/gV1GBbM/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mTrlvofu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/gV1GBbM/image.png" alt="education pack " width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine the GitHub Education Pack as a handy kit of free software development tools and resources from GitHub, designed to make it easier for students to learn and work together on coding projects.&lt;/p&gt;

&lt;p&gt;Students get a variety of resources including code editors, hosting services, cloud credits, and learning platforms. It's all there to help them enhance their skills and work on coding projects effectively-- and all of that at no cost. Yes, you heard it correctly, my friend!&lt;/p&gt;

&lt;p&gt;Let's take a look at some exciting benefits of the GitHub Student Developer Pack and how you can get your hands on them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of the GitHub Education Pack
&lt;/h2&gt;

&lt;p&gt;This pack offers a wide range of deals spanning various tech areas. No matter your specialization, there's probably an offer that will give your career a boost and grant you free access to top-notch developer tools used in that field.&lt;br&gt;
 n short, it's safe to say they've made a significant effort with this pack&lt;/p&gt;

&lt;p&gt;Here are a few examples of what you'll be able to get your hands on:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://azure.microsoft.com/en-us"&gt;Microsoft Azure&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Microsoft provides a collection of cloud services tailored to assist students in building, overseeing, and deploying applications. These services come with free credits, empowering students to utilize the cloud's potential for their development projects.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S71UVvKg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/QD0j7fj/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S71UVvKg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/QD0j7fj/image.png" alt="microsoft azure" width="402" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://bootstrapstudio.io/"&gt;Bootstrap Studio&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;A desktop app for creating responsive websites using the Bootstrap framework&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MVfHuohO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/gJQh2NH/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MVfHuohO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/gJQh2NH/image.png" alt="bootstrap audio" width="400" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://www.educative.io/"&gt;Educative&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Thanks to the GitHub Student Developer pack, students like you can enjoy six months of free access to over 60 courses that cover high-demand subjects. This valuable resource can assist you in mastering coding, enhancing your skills, and even excelling in technical interviews.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nJm8W8PL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/kSzWff0/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nJm8W8PL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/kSzWff0/image.png" alt="educative" width="401" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="//namecheap.com"&gt;Namecheap&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;As a component of the GitHub Student Developer Pack, Namecheap provides a complimentary one-year registration for a &lt;code&gt;.me&lt;/code&gt; domain, complete with an SSL certificate.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---DfVr5Eg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/NyY4nNX/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---DfVr5Eg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/NyY4nNX/image.png" alt="namecheap" width="405" height="515"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://1password.com/"&gt;1Password&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Highlighting the crucial importance of security is essential. Through the Student Developer Pack, you can acquire 1Password, a tool that aids in protecting your accounts. It helps you stay vigilant about password breaches and spot other security issues. &lt;/p&gt;

&lt;p&gt;As a student, this empowers you to maintain the safety of your online accounts, and you'll receive 1Password free of charge for a year.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MKUzKXSt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/GdK7mmq/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MKUzKXSt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/GdK7mmq/image.png" alt="1password" width="409" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The beloved &lt;a href="https://education.github.com/experts"&gt;GitHub campus Experts Program&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;GitHub Campus Expert program&lt;/strong&gt; aims to empower university students, turning them into student leaders who champion technology and open-source collaboration. These Campus Experts are equipped to arrange events, guide fellow students, and cultivate a thriving tech community at their own universities.&lt;/p&gt;

&lt;p&gt;However, it's crucial to remember that having the GitHub Student Developer Pack is a requirement to apply for this program.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wMh_wZ76--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/jZtRs7C/image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wMh_wZ76--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/jZtRs7C/image.png" alt="github campusexperts" width="408" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are plenty more apps and services in the pack, and I'll use them as the need arises. One that caught my eye is &lt;a href="https://polypane.app/"&gt;Polypane&lt;/a&gt;, a browser that assists in web development. They also offer several automated mail services for free, although there may be some usage limits. &lt;a href="https://www.gitkraken.com/"&gt;GitKraken Client&lt;/a&gt; and &lt;a href="https://www.gitkraken.com/gitlens/pro-features"&gt;GitLens+&lt;/a&gt; are incredibly handy tools for Git work, and now I have access to them, along with &lt;a href="https://www.git-tower.com/windows"&gt;Tower&lt;/a&gt;, another popular Git client. There are multiple icon services available, and I can enjoy credits for &lt;a href="https://azure.microsoft.com/en-us"&gt;Microsoft Azure&lt;/a&gt;, &lt;a href="https://www.digitalocean.com/try/developer-brand?utm_campaign=emea_brand_kw_en_cpc&amp;amp;utm_adgroup=digitalocean_exact_exact&amp;amp;_keyword=digital%20ocean&amp;amp;_device=c&amp;amp;_adposition=&amp;amp;utm_content=conversion&amp;amp;utm_medium=cpc&amp;amp;utm_source=google&amp;amp;gad=1&amp;amp;gclid=CjwKCAjw69moBhBgEiwAUFCx2M4nqofDUY3kZalbEb4PBG4wdrWWeRY4UHjNEr4UEXg6DYUSYx6RJhoCXZgQAvD_BwE"&gt;DigitalOcean&lt;/a&gt;, and &lt;a href="https://www.heroku.com/"&gt;Heroku&lt;/a&gt;. With the pack, I can install apps that assess and enhance code performance and design, as well as testing tools, no-code services, and CI platforms. There are discounts on hardware from &lt;a href="https://www.adafruit.com/"&gt;Adafruit&lt;/a&gt;, free domains, and a variety of analysis and marketing tools as well.&lt;/p&gt;

&lt;p&gt;Being a student is all about learning, and the pack has plenty of academic perks. As a Global Campus student, you get six months of &lt;a href="https://frontendmasters.com/"&gt;Frontend Masters&lt;/a&gt; and &lt;a href="https://www.educative.io/"&gt;Educative&lt;/a&gt;,  and some weeks of InterviewCake&lt;a href="https://www.interviewcake.com/"&gt;enter link description here&lt;/a&gt;, all for free. You can also access &lt;a href="https://learn.mongodb.com/"&gt;MongoDB University&lt;/a&gt; credit and three months of &lt;a href="https://www.jetbrains.com/academy/"&gt;JetBrains Academy&lt;/a&gt; related to the offered products.&lt;/p&gt;

&lt;p&gt;Bruh..&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/2UzgBibcqFZnTnSGf9/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/2UzgBibcqFZnTnSGf9/giphy.gif" alt="stuffed" width="500" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Overall, this pack offers students a valuable collection of crucial developer tools and resources that are often costly or come with subscription fees, all for free. Utilizing and mastering these tools can be a significant step forward in advancing your career.&lt;/p&gt;

&lt;p&gt;There's an abundance of these fantastic products available for free; you simply have to explore them for yourself, my friend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;To be eligible for all these fantastic offers, you just need to meet these requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to be at least 13 years old.&lt;/li&gt;
&lt;li&gt;You should have a GitHub user account.&lt;/li&gt;
&lt;li&gt;You need either a verifiable school email address or valid documents proving you're currently a student.&lt;/li&gt;
&lt;li&gt;When you apply, you must be currently enrolled in a program that leads to a degree or diploma.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To get a better understanding of how to apply and which documents are accepted, please take a look at the detailed &lt;a href="https://docs.github.com/en/education/explore-the-benefits-of-teaching-and-learning-with-github-education/github-global-campus-for-students/apply-to-github-global-campus-as-a-student"&gt;Education documentation.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to apply
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Log in to your &lt;a href="https://github.com/"&gt;GitHub&lt;/a&gt; account or sign up for one if you haven't already.&lt;/li&gt;
&lt;li&gt; If you've already signed up, simply include your school-issued email address in your account's &lt;a href="https://github.com/settings/emails"&gt;Email&lt;/a&gt; settings section.&lt;/li&gt;
&lt;li&gt;Visit the &lt;a href="https://education.github.com/pack"&gt;Education Pack&lt;/a&gt; Section and log in using your GitHub account.&lt;/li&gt;
&lt;li&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ftmjqgNi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://i.ibb.co/sCqNSy5/image.png" alt="enter image description here" width="800" height="347"&gt;
&lt;/li&gt;
&lt;li&gt; Click on Sign Up and fill in all the details as required about your usage of GitHub and your school/college details.&lt;/li&gt;
&lt;li&gt; Click on Submit My Application and wait for approval from the GitHub team.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To sum it up, the GitHub Education Pack is your ace in the hole. With an abundance of free tools and resources at your disposal, It is literally a goldmine of free tools. Seize the opportunity, enhance your skills, and open doors to a world of coding possibilities. Don't overlook this essential resource.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>github</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title># "JavaScript Closures: Demystified."</title>
      <dc:creator>muhd</dc:creator>
      <pubDate>Fri, 22 Sep 2023 08:38:47 +0000</pubDate>
      <link>https://dev.to/muhd/-javascript-closures-demystified-3jda</link>
      <guid>https://dev.to/muhd/-javascript-closures-demystified-3jda</guid>
      <description>&lt;p&gt;In the smoky, dimly lit room of JavaScript, there's a concept lurking in the shadows, waiting to be unveiled. We call it a "closure." Now, hang on to your hats, because we're about to embark on a journey into the heart of this enigmatic creature.&lt;/p&gt;

&lt;p&gt;When functions can be treated as values and local bindings are recreated upon each function call, an intriguing question arises: What occurs with these local bindings when the function call that created them is no longer active?&lt;/p&gt;

&lt;p&gt;For 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 rememberValue(n) {
let local = n;
return () =&amp;gt; local;
}

let wrap1 = rememberValue(1);
let wrap2 = rememberValue(2);
console.log(wrap1());
// → 1
console.log(wrap2());
// → 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is permitted and works as you'd hope  -  both instances of the binding remain accessible. This scenario effectively illustrates that local bindings are created afresh for each function call, and different calls do not interfere with each other's local bindings.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are local bindings?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  First, what are bindings??
&lt;/h3&gt;

&lt;p&gt;In JavaScript, the term 'binding' is the formal language used to describe what many individuals commonly call a &lt;em&gt;variable&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let totalHoursSpentOnArticle = 42;
const name = "Ford Arthur";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bindings that are defined outside of any function or block have a scope that encompasses the entire program. As a result, you can reference such bindings from any part of your code. These bindings are commonly referred to as &lt;em&gt;"global"&lt;/em&gt; bindings.&lt;/p&gt;

&lt;p&gt;Bindings declared using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are, in fact, local to the block where they are declared. Therefore, if you create one of these bindings within a loop, the code segments located before and after the loop lack visibility of it. In the JavaScript version prior to 2015, scopes were only introduced by functions. Consequently, old-style bindings, created with the &lt;code&gt;var&lt;/code&gt; keyword, remained accessible throughout the entirety of the function in which they were defined, or at the global scope if they were not within a function&lt;/p&gt;

&lt;p&gt;Bindings generated for function parameters or declared within a function have a scope limited to that specific function, earning them the name of &lt;em&gt;local bindings&lt;/em&gt;. Upon each function call, fresh instances of these bindings are generated. This separation between functions offers a degree of isolation, where each function call operates within its own unique context (local environment), often requiring minimal awareness of the broader global environment.&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 = 10;
if (true) {
  let y = 20;
  var z = 30;
  console.log(x + y + z);
  // → 60
}

console.log(x + y)
// → Uncaught ReferenceError: y is not defined
// y is not visible here
console.log(x + z);
// → 40
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variables &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;z&lt;/code&gt; are the local bindings in this code sample. &lt;code&gt;x&lt;/code&gt; is a global binding.&lt;/p&gt;

&lt;p&gt;Every scope possesses the capability to &lt;em&gt;"look into"&lt;/em&gt; the scope that surrounds it, making &lt;code&gt;x&lt;/code&gt; visible inside the block within the provided example. However, an exception arises when multiple bindings share the same name  -  under such circumstances, the code can exclusively access the innermost binding with that name. For instance, when the code within the &lt;code&gt;square&lt;/code&gt; function references &lt;code&gt;n&lt;/code&gt;, it specifically refers to its own &lt;code&gt;n&lt;/code&gt; and not the &lt;code&gt;n&lt;/code&gt; in the global scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const square = function(n) {
  return n ** 2;
};

let n = 42;
console.log(square(2));
// → 4
console.log(n);
// → 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This behavior, wherein a variable's scope is dictated by its position within the source code hierarchy, serves as an instance of &lt;em&gt;lexical scoping&lt;/em&gt;. Lexical scoping enables inner functions, like &lt;code&gt;square&lt;/code&gt; in this case, to access variables from their enclosing scopes based on their respective positions within the code's structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical scoping
&lt;/h2&gt;

&lt;p&gt;Closures and lexical scope are frequently intertwined and misunderstood by many in the JavaScript community.&lt;/p&gt;

&lt;p&gt;Lexical scope refers to how nested functions can access variables defined in their enclosing scopes. This behavior is illustrated in the code block above…&lt;/p&gt;

&lt;h2&gt;
  
  
  Now, let's direct our gaze to the central element: Closures.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is a closure?&lt;/strong&gt;&lt;br&gt;
A closure is the combination of a function enveloped with references to its surrounding context (the lexical environment). To put it differently, closure provides the means to reach the scope of an outer function from within an inner function.&lt;/p&gt;

&lt;p&gt;To make use of a closure, create a function within another function and make it accessible. You can render a function accessible by either returning it or passing it to another function.&lt;/p&gt;

&lt;p&gt;Even after the outer function has been completed and returned, the inner function will retain access to the variables in the outer function's scope.&lt;/p&gt;

&lt;p&gt;Note the first part of the definition; &lt;em&gt;&lt;em&gt;"A closure is the combination of a function enveloped with references to its surrounding context"&lt;/em&gt;&lt;/em&gt;. That's essentially a description of lexical scope!&lt;/p&gt;

&lt;p&gt;But we require the inclusion of the second part of this definition to give an example of a closure… &lt;em&gt;&lt;em&gt;"Even after the outer function has been completed and returned, the inner function will retain access to the variables in the outer function's scope."&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let us examine fascinating instances of closures;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function theAnswer() {
  var say = () =&amp;gt; console.log(answer); 
  // Local variable that ends up within the closure 
  var answer = "'Forty-two,' said Deep Thought";
  return say;
}
var tellUs = theAnswer(); 
tellUs(); // ''Forty-two,' said Deep Thought'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we have two functions;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;An outer function &lt;code&gt;theAnswer&lt;/code&gt; has a variable &lt;code&gt;answer&lt;/code&gt; and returns the inner function &lt;code&gt;say&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An inner function that returns the variable &lt;code&gt;answer&lt;/code&gt; when &lt;code&gt;tellUs&lt;/code&gt; is invoked…&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---eCRqahF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjd4fsocwwbzhstprssk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---eCRqahF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjd4fsocwwbzhstprssk.png" alt="Image man with red beams coming out of his head..." width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Duplicate the provided code example and give it a go&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's attempt to understand and comprehend what's unfolding…&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the code example;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;theAnswer&lt;/code&gt; is called and creates an environment in which it defines a local variable answer and returns an inner function say.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, we call the &lt;code&gt;theAnswer&lt;/code&gt; function and save the resulting inner function in the variable &lt;code&gt;tellUs&lt;/code&gt;. Logging the variable &lt;code&gt;tellUs&lt;/code&gt; returns something like this;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ƒ theAnswer() {
  var say = () =&amp;gt; console.log(answer); 
  // Local variable that ends up within the closure 
  var answer = "'Forty-two,' said Deep Thought";
  return say;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;So that when we later call &lt;code&gt;tellUs&lt;/code&gt;, it proceeds to run the inner function say, which subsequently logs the value of &lt;code&gt;answer&lt;/code&gt; to the console.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'Forty-two,' said Deep Thought
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Getting your head around programs like these requires a bit of mental gymnastics. Picture function values as these packages carry both their inner workings and the surroundings they grew up in. When you evoke them, they bring that homey environment they were born in - no matter where you make the call from.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use cases of closures
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Data Privacy
&lt;/h3&gt;

&lt;p&gt;Closures can be used to create private variables and functions. This ensures that certain data is not accessible from outside the scope, providing a level of data privacy and encapsulation. &lt;br&gt;
Here's an illustration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function createCounter() {
  let count = 0; // Private variable enclosed by the closure

  return function() {
    count++;
    return count;
  };
}

const incrementCounter = createCounter();
console.log(incrementCounter()); // Output: 1
console.log(incrementCounter()); // Output: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Allow me to offer a succinct breakdown of its operation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;createCounter&lt;/code&gt; is a function that initializes by establishing a private variable named count with an initial value of 0 within its own scope.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It returns an enclosed function, a closure, which, when called, increases the &lt;code&gt;count&lt;/code&gt; by 1 and returns the current value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;incrementCounter&lt;/code&gt; is assigned the result of invoking &lt;code&gt;createCounter&lt;/code&gt;, which means it holds the inner function (closure) that was returned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When &lt;code&gt;incrementCounter()&lt;/code&gt; is called, it triggers the execution of the inner function, resulting in the increase of the &lt;code&gt;count&lt;/code&gt; and the return of the updated value.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With every further use of &lt;code&gt;incrementCounter()&lt;/code&gt;, &lt;code&gt;count&lt;/code&gt; increments.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Callback functions
&lt;/h3&gt;

&lt;p&gt;Closures are often used in dealing with asynchronous tasks and event-driven programming. They help create callback functions that remember and work with their surrounding environment, which is crucial when explaining event-based systems in documentation.&lt;/p&gt;

&lt;p&gt;Here's a code example demonstrating closures in callback functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fetchData(url, callback) {
  setTimeout(function() {
    const data = `Data from ${url}`;
    callback(data); // Closure captures 'data'
  }, 1000); // Simulate a 1-second delay
}

function processData(data) {
  console.log(`Processing: ${data}`);
}

fetchData('https://example.com/api/data', processData);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In our scenario, the &lt;code&gt;fetchData&lt;/code&gt; function simulates an asynchronous data retrieval process. It accepts both a URL and a callback function as its parameters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inside the &lt;code&gt;setTimeout&lt;/code&gt;, it generates some data and invokes the callback function, which captures the data variable from its surrounding scope due to closure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;processData&lt;/code&gt; function serves as our callback, responsible for handling and logging the data that we receive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We call &lt;code&gt;fetchData&lt;/code&gt; with a URL and the &lt;code&gt;processData&lt;/code&gt; callback, demonstrating how closures empower the callback to reach and employ the &lt;code&gt;data&lt;/code&gt; variable from its surrounding scope.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Closures are a fundamental component of functional programming and may be found everywhere in JavaScript as well as in other languages (not only functional ones).&lt;/p&gt;

&lt;p&gt;More use cases are;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;h3&gt;
  
  
  Memoization
&lt;/h3&gt;&lt;/li&gt;
&lt;li&gt;&lt;h3&gt;
  
  
  Partial Applications and currying etc…
&lt;/h3&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can definitely check out more here on &lt;a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)#Applications"&gt;wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Clos(ure)ing remarks, or thoughts? lmao 😂
&lt;/h2&gt;

&lt;p&gt;So, there you have it - the tale of JavaScript closures. They may seem cryptic at first, but once you've grasped their essence, you'll find they're a powerful tool in your programming arsenal. Use them wisely, and you'll unlock new realms of possibility in your technical journey. Happy coding, my friends.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Rd7pEbE7rjZz8vySuU/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Rd7pEbE7rjZz8vySuU/giphy.gif" alt="alt arnold gif" width="480" height="252"&gt;&lt;/a&gt;:&lt;/p&gt;

&lt;h2&gt;
  
  
  Okay real quick
&lt;/h2&gt;

&lt;p&gt;What would this return??&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(var i = 0; i &amp;lt; 3; i++) {
  const log = () =&amp;gt; {
    console.log(i);
  }

  setTimeout(log, 10);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  More resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;MDN&lt;/a&gt; has a fantastic page on closures, as you might imagine, and the &lt;a href="https://en.wikipedia.org/wiki/Closure_(computer_programming)#Applications"&gt;Wikipedia&lt;/a&gt; article on the topic goes into great length regarding further applications as well as how they function in other languages.&lt;/p&gt;

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