<?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: Akindele Emmanuel</title>
    <description>The latest articles on DEV Community by Akindele Emmanuel (@akindeleemmanuel).</description>
    <link>https://dev.to/akindeleemmanuel</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%2F1055799%2F814f9ec5-02c3-449e-b4f6-3428af56f16c.jpg</url>
      <title>DEV Community: Akindele Emmanuel</title>
      <link>https://dev.to/akindeleemmanuel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akindeleemmanuel"/>
    <language>en</language>
    <item>
      <title>Understanding JavaScript closures: variables, functions, and scope.</title>
      <dc:creator>Akindele Emmanuel</dc:creator>
      <pubDate>Sun, 03 Nov 2024 21:02:07 +0000</pubDate>
      <link>https://dev.to/akindeleemmanuel/understanding-javascript-closures-variables-functions-and-scope-l3e</link>
      <guid>https://dev.to/akindeleemmanuel/understanding-javascript-closures-variables-functions-and-scope-l3e</guid>
      <description>&lt;p&gt;“Javascript closures  are  a fundamental concept in programming, enabling functions to access and manipulate variables from surrounding scopes. This powerful technique allows for data encapsulation, context preservation, and efficient memory management. Understanding Javascript closures is crucial for developing robust, scalable, and maintainable applications. In this article, we will look into the complexity of this concept, exploring variables, functions, and scope to master this essential Javascript concept.”. &lt;br&gt;
Now let's see what a closure in Javascript is. I will like to define this in three ways: in a simple and concise way, in a technical and detailed definition, and in a conceptual and analogous definition.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is closure in JavaScript?
&lt;/h2&gt;

&lt;p&gt;“A closure is a/are channel(s) through which function inside another function  have access to its own scope and the scope of its outer function(s), even when the outer function(s) have returned.”.&lt;/p&gt;

&lt;p&gt;“A closure can also be referred to as a self-contained function that maintains a reference to its surrounding lexical scope, allowing it to access and manipulate variables from that scope, even when the function is invoked outside its original context.”.&lt;/p&gt;

&lt;p&gt;“A closure is like a room with memory. When a function is created inside another function, it inherits the outer function’s scope, much like a room inherits the features of the building it’s in. Even when the outer function ‘closes’ (returns), the inner function remembers the outer scope, allowing it to access its variables and functions". These definitions highlight different aspects of JavaScript closure.&lt;/p&gt;

&lt;p&gt;To buttress the definitions or  for better understanding of the subject matter, think of a closure like this. Imagine you have a toy box container where you keep all your favorite toys (the toy box container is our outer function). Now imagine that inside the toy container (outerfunction) you have different toys like cars, dolls, and a robot, among other things. (these are variables or any other function) out of these toys, you have a special toy (inner function) which is the robot. Imagine the robot has special powers through a channel; it can play or connect all the other toys inside the toy box even when the toy box is closed. Imagine that special channel or power to be closure. I hope you get that. Let's see an example of a code to illustrate closure.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;details&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;surName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john&lt;/span&gt;&lt;span class="dl"&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;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mercy&lt;/span&gt;&lt;span class="dl"&gt;'&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;displayDetails&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="s2"&gt;`hey &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;surName&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="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; you have been registered`&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="nf"&gt;displayDetails&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="nf"&gt;details&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the &lt;code&gt;displayDetails&lt;/code&gt; is the function  or the closure, or better still, referring to the illustration before the code. &lt;code&gt;funtion&lt;/code&gt;     &lt;code&gt;displayDetails&lt;/code&gt; is using the power of closure to access the variables outside it’s scope. Here is the thing. &lt;code&gt;displayDetails&lt;/code&gt; is defined inside &lt;code&gt;details&lt;/code&gt;, it accesses &lt;code&gt;surName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; from the outer scope, and it retains this access even outside its scope. Or, in another way, &lt;code&gt;details&lt;/code&gt; create a scope with &lt;code&gt;surName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt;, &lt;code&gt;displayDetails&lt;/code&gt; access this scope, &lt;code&gt;displayDetail&lt;/code&gt; returns a string using &lt;code&gt;surName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt;,  and &lt;code&gt;details&lt;/code&gt; returns &lt;code&gt;displayDetails&lt;/code&gt;’s result. &lt;code&gt;console.log(details());&lt;/code&gt;  calls &lt;code&gt;displayDetails&lt;/code&gt;, accessing outer scope variables. &lt;br&gt;
 The output of this code is &lt;code&gt;hey john mercy&lt;/code&gt;, &lt;code&gt;you have been registered&lt;/code&gt;&lt;br&gt;
The result is shown below &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flnp8yhd1ypk3adok0jdm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flnp8yhd1ypk3adok0jdm.png" alt="the image display the output of the to explain what a closure is" width="320" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you understand closure, let's see the benefit of closure in Javascript code writing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Variables in closures
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Local variables&lt;/em&gt;&lt;/strong&gt;: Variables declared within function, is accessible only  within the function scope and not accessible outside the scope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Outer variables&lt;/em&gt;&lt;/strong&gt;: Variables declared in an outer function are accessible to inner function &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Global Variables&lt;/em&gt;&lt;/strong&gt;: Variables  declared outside all functions, accessible from anywhere &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Functions in closure
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Inner functions&lt;/em&gt;&lt;/strong&gt;: these are functions defined within another function that have access to outer scope&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Outer function&lt;/em&gt;&lt;/strong&gt;: this function defines the scope for inner functions &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Function expressions&lt;/em&gt;&lt;/strong&gt;: function defined as expressions can be used as closures &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Scope in closures
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Lexical scoping&lt;/em&gt;&lt;/strong&gt;:  inner functions have access to outer scope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Dynamic scoping&lt;/em&gt;&lt;/strong&gt;: scope is determined at runtime ( not used in javascript). &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What are the benefits of closure?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Data hiding&lt;/em&gt;&lt;/strong&gt;: protecting data from external access 
Let’s see in the code below example of code that exhibits this data hiding
&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&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;privateName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="nx"&gt;name&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;privateAge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&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="na"&gt;getName&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;privateName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="na"&gt;getAge&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;privateAge&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="na"&gt;setAge&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="nx"&gt;newAge&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nx"&gt;privateAge&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newAge&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="p"&gt;}&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bala daodu&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getName&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;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getAge&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, the &lt;code&gt;function&lt;/code&gt; &lt;code&gt;Person&lt;/code&gt; takes  two arguments: &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;, This &lt;code&gt;function&lt;/code&gt;  creates a new &lt;code&gt;person&lt;/code&gt; &lt;code&gt;object&lt;/code&gt;. Inside the &lt;code&gt;function&lt;/code&gt;, two private &lt;code&gt;variables&lt;/code&gt; are declared: &lt;code&gt;privateName&lt;/code&gt; and &lt;code&gt;privateAge&lt;/code&gt;. These variables are assigned the &lt;code&gt;values&lt;/code&gt; passed to the &lt;code&gt;person function&lt;/code&gt; ( &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;Age&lt;/code&gt;). These variables are private because they’re declared with &lt;code&gt;let&lt;/code&gt; and aren’t directly accessible outside the  &lt;code&gt;Person&lt;/code&gt; &lt;code&gt;function&lt;/code&gt;. The &lt;code&gt;return&lt;/code&gt; objects are also created with three methods. These three methods provide controlled access to private variables. So the &lt;code&gt;Person function&lt;/code&gt; creates a closure, allowing the returned object to access private variables. Also the private variables are hidden from external access, providing data protection. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Encapsulations&lt;/em&gt;&lt;/strong&gt;: bundling data  and methods &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Context preservation&lt;/em&gt;&lt;/strong&gt;: retaining context in asynchronous calls.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common use cases of closure
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Event listeners or event handling&lt;/em&gt;&lt;/strong&gt;: preserving event context. Here is a simple event handling code
&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myButton&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;clickcounter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;myButton&lt;/span&gt;&lt;span class="dl"&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;clickCounterElement&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clickcounter&lt;/span&gt;&lt;span class="dl"&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;clickcounter&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;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
                &lt;span class="nx"&gt;clickcounter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

                &lt;span class="nx"&gt;clickCounterElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`you have clicked &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;clickcounter&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; times`&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/body&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what happened in the code. We define a function &lt;code&gt;createClickCounter&lt;/code&gt; that &lt;code&gt;returns&lt;/code&gt; another &lt;code&gt;function&lt;/code&gt;. Inside &lt;code&gt;createClickCounter&lt;/code&gt;, we declare a &lt;code&gt;variable&lt;/code&gt; &lt;code&gt;clickCounter&lt;/code&gt; initialized to &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
The returned &lt;code&gt;function increments&lt;/code&gt; &lt;code&gt;clickCounter&lt;/code&gt; and updates the text content of &lt;code&gt;clickCounterElement&lt;/code&gt;. We call &lt;code&gt;createClickCounter()&lt;/code&gt; to create a closure and assign it to the counter variable. We attach the &lt;code&gt;counter&lt;/code&gt; &lt;code&gt;function&lt;/code&gt; to the &lt;code&gt;button's&lt;/code&gt; click event using &lt;code&gt;addEventListener&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;counter function&lt;/code&gt; is a closure because it has access to its own scope, has access to the outer function's scope &lt;code&gt;(createClickCounter())&lt;/code&gt;, retains the state of &lt;code&gt;clickCounter&lt;/code&gt; between function calls.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Callback functions&lt;/em&gt;&lt;/strong&gt;: preserving context in asynchronous calls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Private modules&lt;/em&gt;&lt;/strong&gt;: creating private variables and functions &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;JavaScript closures empower developers to write efficient, modular, and secure code. By mastering variables, functions, and scope, developers can harness closures full potential. Closures enable data encapsulation, state preservation, and private variables, elevating code quality and maintainability. With this foundational understanding, developers can tackle complex Javascript challenges with confidence. Effective use of closures is essential for scalable, robust, and efficient JavaScript applications. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Mastering JavaScript Function and Variable Hoisting: Best Practices and Examples.</title>
      <dc:creator>Akindele Emmanuel</dc:creator>
      <pubDate>Wed, 30 Oct 2024 11:14:27 +0000</pubDate>
      <link>https://dev.to/akindeleemmanuel/mastering-javascript-function-and-variable-hoisting-best-practices-and-examples-4cmi</link>
      <guid>https://dev.to/akindeleemmanuel/mastering-javascript-function-and-variable-hoisting-best-practices-and-examples-4cmi</guid>
      <description>&lt;p&gt;"Writing effective, error-free code requires an understanding of JavaScript's special function and variable hoisting behavior. This article delves into the details of hoisting, providing best practices and illustrative examples to enhance developers' proficiency. By mastering hoisting, developers can streamline their coding process and create more robust applications."&lt;/p&gt;

&lt;h2&gt;
  
  
  What is hoisting?
&lt;/h2&gt;

&lt;p&gt;Hoisting is javaScripts default behaviour where variable and function declarations are moved to the top of their scope before the execution of code. &lt;/p&gt;

&lt;p&gt;The JavaScript engine will normally execute code from the top to the button, line by line or one line after the other, and when it does, hoisting happens before execution during the semantic analysis phase. Variable and function declarations are moved to the top of their scope before the execution process. You can confirm this by using &lt;code&gt;console.log()&lt;/code&gt; or &lt;code&gt;document.write()&lt;/code&gt; to log out a function or variable before the code itself. Let us look at the illustrations below to shed light on the explanation above.&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="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nx"&gt;DOCTYPE&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="nx"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;charset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;UTF-8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&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;viewport&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;width=device-width, initial-scale=1.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Document&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/title&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/head&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;   

     &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/body&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/html&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will simply output 12 just as we can see below is the result of the code &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vexrdnjhz58virdw90y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vexrdnjhz58virdw90y.png" alt="an image showing the result of the code with function keyword square in the document when square is invoked" width="515" height="223"&gt;&lt;/a&gt;&lt;br&gt;
Now, you can also call or invoke the function before the function is created, and after it is created, it will still give the same result. Are you surprised? This is only possible because of hoising; now let's see that. You can also try this on your own.&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="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nx"&gt;DOCTYPE&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="nx"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;charset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;UTF-8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&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;viewport&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;width=device-width, initial-scale=1.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Document&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/title&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/head&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&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;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;   

    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/body&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/html&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will still give the same result as above; it will output 12 in the document page when you use &lt;code&gt;document.write()&lt;/code&gt; or in the console when you use &lt;code&gt;console.log()&lt;/code&gt;, just like the example above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fveyg3m5sj2i6fvncjqdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fveyg3m5sj2i6fvncjqdw.png" alt="the image showing the same reult or output when the function square is called or invoked before the function declaration." width="515" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is possible because of hoisting. So hoisting moves &lt;code&gt;declaration&lt;/code&gt; only and &lt;code&gt;initialization&lt;/code&gt; or &lt;code&gt;assignment&lt;/code&gt; are left in place. That means &lt;code&gt;javascript&lt;/code&gt; &lt;code&gt;initializations&lt;/code&gt; are not hoisted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Hoisting.
&lt;/h2&gt;

&lt;p&gt;In this article we will be looking at two types of hoisting, which include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variable Hoisting&lt;/li&gt;
&lt;li&gt;Function Hoisting&lt;/li&gt;
&lt;li&gt;Function Expression Hoisting&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is variable hoisting?
&lt;/h3&gt;

&lt;p&gt;This can simply refer to the behaviour of javaScript to move a variable declaration to the top of its scope, regardless of its actual position in the code. &lt;br&gt;
Just as I have pointed out while explaining hoisting using a &lt;code&gt;function&lt;/code&gt; as an example, variable declarations are also moved the same way the function is moved to the top, but in the case of variables,   the output or result might seem a little bit different and not as direct as the case of function code that can be invoked or called before the actual code and it &lt;code&gt;initializes&lt;/code&gt;. In the case of variable hoisting, different results for different types of variables. Let's check this out! In the example above, we were able to call or invoke a function before the actual code, and we saw the output: let's use the same method using any of the types of variables (&lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, and &lt;code&gt;var&lt;/code&gt;). Let's see in the example below&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="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nx"&gt;DOCTYPE&lt;/span&gt; &lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt; &lt;span class="nx"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;en&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&lt;/span&gt; &lt;span class="nx"&gt;charset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;UTF-8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;meta&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;viewport&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;width=device-width, initial-scale=1.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Document&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/title&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/head&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;let&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;abudog&lt;/span&gt;&lt;span class="dl"&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;goat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;constano&lt;/span&gt;&lt;span class="dl"&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;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vanetino&lt;/span&gt;&lt;span class="dl"&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;name&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;goat&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;dog&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/body&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/html&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above worked without error, below is the result of the code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0scf84qyxb10b1szo74i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0scf84qyxb10b1szo74i.png" alt="the image shows the result of using variable key words such as let, const and var and the results in the console" width="547" height="236"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But let's see what happens if we want to &lt;code&gt;console.log()&lt;/code&gt; our variable declaration before the code or before we write the actual code. For example, let's say we have &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt; separated. Now let's see the different results that we will get. &lt;br&gt;
&lt;strong&gt;&lt;em&gt;Note&lt;/em&gt;&lt;/strong&gt;: just write your code along for this will aid fast understanding of the subject matter.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;        
        &lt;span class="c1"&gt;//  we are looking at `let` variable keyword first&lt;/span&gt;
        &lt;span class="c1"&gt;// to see what will be log out in the console&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;name&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;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;abudog&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// console.log(dog);&lt;/span&gt;
        &lt;span class="c1"&gt;// var dog = "vanetino"; &lt;/span&gt;
    &lt;span class="c1"&gt;//    console.log(goat);&lt;/span&gt;
    &lt;span class="c1"&gt;//    const goat = "constano"; &lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the same principle as we have used in the code with function declaration, just like calling the &lt;code&gt;variable&lt;/code&gt; before actual code. See the output below; you can as well try this on your own&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F540au7yk1fs61b0u15yi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F540au7yk1fs61b0u15yi.png" alt="the image of the result for using variable key word let, console.log() before the declaration of the code" width="531" height="152"&gt;&lt;/a&gt;&lt;br&gt;
The result is &lt;code&gt;uncaught referenceError: cannot access ‘name’ before initialization&lt;/code&gt;&lt;br&gt;
Let's also see for &lt;code&gt;const&lt;/code&gt; variable  what will happen&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;        
        &lt;span class="c1"&gt;//  We are looking at using `const` variable keyword now&lt;/span&gt;
        &lt;span class="c1"&gt;// to see what will be logged out in the console&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;goat&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;goat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;constano&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// console.log(name);&lt;/span&gt;
        &lt;span class="c1"&gt;// let name = "abudog";&lt;/span&gt;
        &lt;span class="c1"&gt;// console.log(dog);&lt;/span&gt;
        &lt;span class="c1"&gt;// var dog = "vanetino";&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is the output from the &lt;code&gt;console&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60yqg5ni6rr9eidl6y5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60yqg5ni6rr9eidl6y5f.png" alt="the image shows the rsult of the code called before the declaration using const variable keyword" width="542" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we try &lt;code&gt;var&lt;/code&gt; variable keyword and let's see the result&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;        
        &lt;span class="c1"&gt;//  Now we are using `var` variable keyword&lt;/span&gt;
        &lt;span class="c1"&gt;// to see what will be logged out in the console&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;dog&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;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vanetino&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="c1"&gt;//    console.log(goat);&lt;/span&gt;
    &lt;span class="c1"&gt;//    const goat = "constano";&lt;/span&gt;
    &lt;span class="c1"&gt;//    let name = "abudog";&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffg5g15lvoky1kvnja3ff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffg5g15lvoky1kvnja3ff.png" alt="the image shows the result when hoisting when you use var variable key word" width="542" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the case of &lt;code&gt;var&lt;/code&gt;  variable keyword, the result is undefine. &lt;/p&gt;

&lt;p&gt;Now we can see that the output is different from the first example, where a &lt;code&gt;function&lt;/code&gt; keyword is used to declare the function. Variable hoisting is peculiar, and not only that, we saw that &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables output different results and &lt;code&gt;var&lt;/code&gt; variable output &lt;code&gt;undefined&lt;/code&gt;. Now let's see what happened. The &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; declaration is hoisted to the top of their scope, creating a &lt;code&gt;variable&lt;/code&gt; &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;goat&lt;/code&gt; with an &lt;code&gt;uninitialized&lt;/code&gt; &lt;code&gt;value&lt;/code&gt;. &lt;code&gt;Let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables are not initialized until they are declared; they are in a &lt;strong&gt;&lt;em&gt;temporary&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;dead&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;zone&lt;/em&gt;&lt;/strong&gt; from the start of the scope until the declaration accessing a &lt;code&gt;variable&lt;/code&gt; in the &lt;strong&gt;&lt;em&gt;temporary&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;dead&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;zone&lt;/em&gt;&lt;/strong&gt;, so this results in a &lt;code&gt;referenceError&lt;/code&gt;. When you try to log &lt;code&gt;name&lt;/code&gt; for &lt;code&gt;let&lt;/code&gt; and  &lt;code&gt;goat&lt;/code&gt;  for &lt;code&gt;const&lt;/code&gt; &lt;code&gt;variable&lt;/code&gt;,  they are still in the &lt;strong&gt;&lt;em&gt;TDZ&lt;/em&gt;&lt;/strong&gt;, so &lt;code&gt;javaScript&lt;/code&gt; throws a &lt;code&gt;referenceError&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why doesn't this happen with var?
&lt;/h3&gt;

&lt;p&gt;Variables declared with &lt;code&gt;var&lt;/code&gt; are also hoisted, but they are &lt;code&gt;initialized&lt;/code&gt; with &lt;code&gt;undefined&lt;/code&gt; immediately. This behaviour is by &lt;code&gt;default&lt;/code&gt; for &lt;code&gt;var&lt;/code&gt; variable before its declaration. Just like we can see in the last example above.&lt;br&gt;
 Don't forget the hoisting processes occur behind the scenes, and it gave birth to the results we can see or make the process work out the way we saw them.  &lt;/p&gt;
&lt;h2&gt;
  
  
  Function and Function Expression Hoisting
&lt;/h2&gt;
&lt;h3&gt;
  
  
  What is function hoisting?
&lt;/h3&gt;

&lt;p&gt;Function hoisting is a javaScript behaviour where function declarations are moved to the top of their scope, regardless of where they’re actually defined. This allows functions to be called before they’re &lt;code&gt;defined&lt;/code&gt;. I refer to the first example at the beginning of this article.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
     &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&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;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function declaration vs function expression
&lt;/h2&gt;

&lt;p&gt;Before diving into function expression hoisting, let’s quickly review the difference between function declarations and function expressions:&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Function declaration&lt;/em&gt;&lt;/strong&gt;: a function declaration defined using the &lt;code&gt;keyword&lt;/code&gt;, for example, &lt;code&gt;function&lt;/code&gt; &lt;code&gt;add( a, b) { &lt;br&gt;
return a + b; }&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Function expression&lt;/em&gt;&lt;/strong&gt;:  a function defined as an expression, often assigned to a &lt;code&gt;variable&lt;/code&gt;; for example, &lt;code&gt;const&lt;/code&gt; &lt;code&gt;add = function(a, b) { return a + b; };&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here is the important thing or part: &lt;code&gt;function&lt;/code&gt; &lt;code&gt;expressions&lt;/code&gt; are not hoisted in the same way as function declarations. Only the variable declaration is hoisted, not the assignment. Consider the example below&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="c1"&gt;// this is a function declaration&lt;/span&gt;
         &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;//  this is a function expression&lt;/span&gt;
         &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addConst&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;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;output of function&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;addConst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;output of function expression addConst&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two functions here: &lt;code&gt;function declaration&lt;/code&gt; with the keyword &lt;code&gt;function&lt;/code&gt; and &lt;code&gt;function expression&lt;/code&gt; declared using the keyword &lt;code&gt;const&lt;/code&gt;, these two should log out the same result. This is the result below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphtolw3gpiuh1g69z2kr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphtolw3gpiuh1g69z2kr.png" alt="the image shows the result of function declaration with function keyword and a function expression" width="527" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's see what happens if we try to &lt;code&gt;console.log()&lt;/code&gt; the two codes: the one with &lt;code&gt;function declaration&lt;/code&gt; and the other with &lt;code&gt;function expression&lt;/code&gt;. Using the same code.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="c1"&gt;// This is a function&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;output of function&lt;/span&gt;&lt;span class="dl"&gt;"&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;//  This is a function expression&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;addConst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;output of function expression addConst&lt;/span&gt;&lt;span class="dl"&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;addConst&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;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="p"&gt;};&lt;/span&gt;         
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the output of the code &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fssz78ab2411vu701a4sv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fssz78ab2411vu701a4sv.png" alt="this image shows the result of function declaration and function expression when they are called before their codes" width="547" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the first code with &lt;code&gt;function declaration&lt;/code&gt; worked very fine, and the second code with &lt;code&gt;function expression&lt;/code&gt; using the &lt;code&gt;const&lt;/code&gt; keyword to declare it displayed an &lt;code&gt;uncaught ReferenceError: cannot access addConst’ before initialization&lt;/code&gt;.   This is because only the &lt;code&gt;variable declaration&lt;/code&gt; is hoisted in variables &lt;code&gt;defined&lt;/code&gt; using &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; and not the &lt;code&gt;assignment&lt;/code&gt;. So you can't call the &lt;code&gt;function&lt;/code&gt; for &lt;code&gt;function expression&lt;/code&gt; before it’s &lt;code&gt;assigned&lt;/code&gt; to the variable. It is only possible when writing code with a &lt;code&gt;function&lt;/code&gt; keyword.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best practices for avoiding Hoisting issues
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Declare variables at the top of their scope&lt;/em&gt;&lt;/strong&gt;: 
Below are very good examples of &lt;code&gt;declaring&lt;/code&gt; &lt;code&gt;variable&lt;/code&gt; or &lt;code&gt;function&lt;/code&gt; at the top of their scope before usage
&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="c1"&gt;// Hoisting with function declaration&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;salute&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, Emmanue!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Hoisting with var&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&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;meet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The variable `name` is hoisted, but it's undefined here until it is assigned a value&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Calling functions before they are defined&lt;/span&gt;
&lt;span class="nf"&gt;meet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;         &lt;span class="c1"&gt;// Output: Hello, world!&lt;/span&gt;
&lt;span class="nf"&gt;salute&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;     &lt;span class="c1"&gt;// Output: Hello, undefined&lt;/span&gt;
&lt;span class="c1"&gt;// Assigning a value to the variable `name`&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;Emmanuel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Calling the function again after assigning a value&lt;/span&gt;
&lt;span class="nf"&gt;meet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;     &lt;span class="c1"&gt;// Output: Hello, Emmanuel&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Use Let and Const for block scope&lt;/em&gt;&lt;/strong&gt;:
Always use &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variable for block scope 
Example: in javaScript, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are used for &lt;code&gt;block-scoped&lt;/code&gt; variable declarations, which means they are limited to the &lt;code&gt;block&lt;/code&gt; in which they are defined either within a &lt;code&gt;loop&lt;/code&gt;, &lt;code&gt;condition&lt;/code&gt;, or a &lt;code&gt;function&lt;/code&gt; 
In the code below we can see how to use this
&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;blockScopeExample&lt;/span&gt;&lt;span class="p"&gt;()&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;blockScopedVariable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am block scoped!&lt;/span&gt;&lt;span class="dl"&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;blockScopedConstant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am a constant block scoped!&lt;/span&gt;&lt;span class="dl"&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;blockScopedVariable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: I am block scoped!&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;blockScopedConstant&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// Output: I am a constant block scoped!&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Trying to access blockScopedVariable and blockScopedConstant here will result in a ReferenceError&lt;/span&gt;
    &lt;span class="c1"&gt;// console.log(blockScopedVariable); // Uncaught ReferenceError: blockScopedVariable is not defined&lt;/span&gt;
    &lt;span class="c1"&gt;// console.log(blockScopedConstant);  // Uncaught ReferenceError: blockScopedConstant is not defined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;blockScopeExample&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Example with a loop&lt;/span&gt;
&lt;span class="k"&gt;for &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;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="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;`Inside loop: &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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Inside loop: 0, Inside loop: 1, Inside loop: 2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Trying to access `i` here will also result in a ReferenceError&lt;/span&gt;
&lt;span class="c1"&gt;// console.log(i); // Uncaught ReferenceError: i is not defined&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/script&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Avoid re-defining variables in the same scope&lt;/em&gt;&lt;/strong&gt;: &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;Minimize global variables&lt;/em&gt;&lt;/strong&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Mastering JavaScript function and variable hoisting is crucial for any developer aiming to write clean, efficient, and error-free code. By understanding how hoisting works, particularly with var, let, and const, you can avoid common pitfalls related to variable scope and initialization.&lt;br&gt;
Adopting best practices such as declaring variables at the top of their scope, using let and const for block scope, and favoring function declarations where appropriate will not only enhance the readability of your code but also prevent unexpected behaviors that can arise from hoisting.&lt;br&gt;
As you continue to explore JavaScript, keep these principles in mind and apply them in your projects. With practice, you'll gain a deeper understanding of how hoisting impacts your code, enabling you to leverage this knowledge for more effective programming. Embrace these practices, and you'll be well on your way to becoming a proficient JavaScript developer, equipped to tackle complex challenges with confidence.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>CSS Positioning: Simplified for Beginners</title>
      <dc:creator>Akindele Emmanuel</dc:creator>
      <pubDate>Fri, 22 Sep 2023 08:31:32 +0000</pubDate>
      <link>https://dev.to/akindeleemmanuel/css-positioning-simplified-for-beginners-8mi</link>
      <guid>https://dev.to/akindeleemmanuel/css-positioning-simplified-for-beginners-8mi</guid>
      <description>&lt;p&gt;CSS positioning is the placement of an element at a desired location utilizing the top, bottom, right, and left attributes, which determine the element's final position.&lt;/p&gt;

&lt;p&gt;Imagine 20 students are resuming into an empty classroom of 20 sit, the teacher has the ability to position any of this students in the front sit or the back sit, since he/she has the ability to do so, just with his command  go there the student we occupy any where in the direction the teacher points to or direct them to sit. Also  in CSS, the syntax is the teacher and the class is the parent element while the students are the child element.&lt;/p&gt;

&lt;p&gt;For example: &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosgegs9mj7uqe96afx5i.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosgegs9mj7uqe96afx5i.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The original position of the element is top-left. We can now change the position to right or center or anywhere  using the &lt;code&gt;position&lt;/code&gt; property and appropriate value in your code.  The element will be re-positioned.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F74w7qwf5uz4aunj4zxru.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F74w7qwf5uz4aunj4zxru.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because the position attribute was added to the code above, the element's position has now moved away from its original position. &lt;/p&gt;

&lt;p&gt;Depending on where you want the element, this can be done not only to the left but also to the desired top or bottom. Let's now understand more about the CSS positioning values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Values  of  CSS positioning
&lt;/h2&gt;

&lt;p&gt;In CSS, there are five major values of positioning. They include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Relative&lt;/li&gt;
&lt;li&gt;Absolute&lt;/li&gt;
&lt;li&gt;Sticky &lt;/li&gt;
&lt;li&gt;Fixed and &lt;/li&gt;
&lt;li&gt;static.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Relative position value
&lt;/h3&gt;

&lt;p&gt;This is a positioning style that is used in styling an element by moving it from its initial position to the top, bottom, right, or left, depending on the location you want the element to be. &lt;/p&gt;

&lt;p&gt;Generally, this has no influence on the position of the other elements except the element that you are designing. Using this &lt;code&gt;relative&lt;/code&gt; positioning style without specifying whether &lt;code&gt;left&lt;/code&gt; or &lt;code&gt;right&lt;/code&gt;, in which case the element assumes its regular position and it has no effect.  This can be archived by adding &lt;code&gt;position&lt;/code&gt; property with value as &lt;code&gt;relative&lt;/code&gt; to the CSS styling. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9op9f1bjoii3v6fu45d.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9op9f1bjoii3v6fu45d.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above shows the default position of the elements and the Code Pen below shows what happens when the CSS &lt;code&gt;position&lt;/code&gt; property and the &lt;code&gt;relative&lt;/code&gt; value is added to one of the elements&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Emmyokans/embed/dywJjJw?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The position of the red element has changed from the default position.&lt;/p&gt;

&lt;h3&gt;
  
  
  Absolute Position value
&lt;/h3&gt;

&lt;p&gt;This positioning style allows you to position an element relative to the parent element or the HTML page itself in the absence of the parent element. &lt;/p&gt;

&lt;p&gt;Imagine you have a big  square as the parent element and also have another small square as the child element  in the big square. You will notice that  both parent and child elements are positioned by default to the top-left side.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjuxpjw01pkwmqhs61ht7.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjuxpjw01pkwmqhs61ht7.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can use the &lt;code&gt;position&lt;/code&gt; of value &lt;code&gt;absolute&lt;/code&gt; to position the child element (the small square) anywhere inside the big square by giving the parent element a &lt;code&gt;position&lt;/code&gt; with value &lt;code&gt;relative&lt;/code&gt;. Then, the child element will position relatively anywhere inside the parent element using either the &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;left&lt;/code&gt;, &lt;code&gt;right&lt;/code&gt; or &lt;code&gt;bottom&lt;/code&gt; elements. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faq6oxan3j2h01yimixv5.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faq6oxan3j2h01yimixv5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now you can see that the child element has changed position to almost the center of the parent element. Position absolute has been used to repositioned it .&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Emmyokans/embed/abPEjxR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;NOTE: Position &lt;code&gt;relative&lt;/code&gt;and &lt;code&gt;absolute&lt;/code&gt;work the same way except that we use &lt;code&gt;relative&lt;/code&gt;to identify the parent element. And &lt;code&gt;absolute&lt;/code&gt;to identify the children element.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sticky Position value
&lt;/h3&gt;

&lt;p&gt;As the name implies, this positioning style allows elements to become sticky while scrolling on the viewpoint. It is a bit different from the previously explained styles but trust it is a special one. &lt;/p&gt;

&lt;p&gt;The element that possesses this style or positioning will maintain the relative positioning along at first but immediately become &lt;strong&gt;sticky&lt;/strong&gt; or &lt;strong&gt;fixed&lt;/strong&gt; when it reaches the specified point white scrolling.  That is after scrolling to a certain point on our screen, this value will fix the position of our element on the screen so it doesn't move.&lt;/p&gt;

&lt;p&gt;Consider the code below. And try it &lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Emmyokans/embed/VwqyBoo?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Fixed position value
&lt;/h3&gt;

&lt;p&gt;This positioning style allows the element to maintain its relative position and remain fixed even when you scroll the viewpoint. This are majorly used for navigation bar in web designs. the element is fixed to its position when you scroll the page.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/Emmyokans/embed/NWeXLPp?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;CSS positioning, though initially daunting, can be made super simple for beginners with the right guidance and practice. &lt;/p&gt;

&lt;p&gt;Understanding the basic concepts of relative, absolute, fixed, and static positioning, as well as their practical applications, empowers beginners to enhance the layout and design of their web projects. &lt;/p&gt;

&lt;p&gt;With continued learning and hands-on experience, beginners can confidently harness the power of CSS positioning to create visually appealing and well-structured web pages.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>css</category>
    </item>
  </channel>
</rss>
