<?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>Forem: jabo Landry </title>
    <description>The latest articles on Forem by jabo Landry  (@jaboarnoldlandry).</description>
    <link>https://forem.com/jaboarnoldlandry</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%2F3557442%2F8d624aa9-26b0-49a4-86e1-812e610eede2.png</url>
      <title>Forem: jabo Landry </title>
      <link>https://forem.com/jaboarnoldlandry</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jaboarnoldlandry"/>
    <language>en</language>
    <item>
      <title>You’re probably writing complex CSS for no reason</title>
      <dc:creator>jabo Landry </dc:creator>
      <pubDate>Sat, 21 Mar 2026 18:46:22 +0000</pubDate>
      <link>https://forem.com/jaboarnoldlandry/youre-probably-writing-complex-css-for-no-reason-1518</link>
      <guid>https://forem.com/jaboarnoldlandry/youre-probably-writing-complex-css-for-no-reason-1518</guid>
      <description>&lt;p&gt;These past days I’ve noticed that many of us haven’t been checking out on what’s new with CSS features and I think if you haven’t checked for these new features, you’re probably complicating your CSS snippets and writing complicated JavaScript snippets that can done using CSS.&lt;/p&gt;

&lt;p&gt;In Today’s guide I will be talking about some of the new features of CSS that you probably haven’t heard of or may have heard of them but haven’t practiced them, no worries I will be guiding you on how they work and giving an example of their use-cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features we will cover
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;has&lt;/code&gt; pseudo-class&lt;/li&gt;
&lt;li&gt;
CSS Nesting

&lt;ul&gt;
&lt;li&gt;Nesting for pseudo-elements and pseudo-classes&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;code&gt;@scope&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;@container&lt;/code&gt; queries&lt;/li&gt;

&lt;li&gt;Summary&lt;/li&gt;

&lt;li&gt;Final Thoughts&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;has&lt;/code&gt;  pseudo-class
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;has&lt;/code&gt; pseudo-class unlocks a lot of possibility with CSS, now with &lt;code&gt;has&lt;/code&gt;  pseudo-class you can style the parent element based on its descendant's behavior, styling and even their attributes.&lt;/p&gt;

&lt;p&gt;If I we were to have a button that toggles a text on/off, in this case we would use JavaScript &lt;code&gt;classList&lt;/code&gt;  method on the element to trigger its visibility by adding and removing the class to hide/show the element that we want to toggle, we would have something that looks like this:&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%2Fkpcbcfdlkhhtcjyj9dxl.gif" 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%2Fkpcbcfdlkhhtcjyj9dxl.gif" alt=" " width="1280" height="718"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With has we don’t need to write any JavaScript at all, But here we take a different approach instead of using a button in our markup as we normally do we use an input box of a checkbox and style it to look more like a button and then we use the parent to check if there’s a checked element in its descendants elements if so we target the p  element and set its display behavior to none, take a look at this video too:&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%2Flnmighk5t3y6aj92zw7t.gif" 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%2Flnmighk5t3y6aj92zw7t.gif" alt=" " width="1280" height="720"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;snippet&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;appearance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;"Hide"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.4rem&lt;/span&gt; &lt;span class="m"&gt;1.4rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.4rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:checked::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;"show"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:active:before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;limegreen&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptatum
      temporibus molestiae itaque harum ut consectetur odit, sit cumque quae
      fugiat error inventore, perspiciatis tempore, voluptatibus magni
      laudantium quas aliquid. Nulla voluptate ex ut molestiae exercitationem
      autem doloribus modi? Voluptatem, repudiandae.
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are the snippet that make up this function, we’ve wrapped all the contents inside the &lt;code&gt;body&lt;/code&gt;  element which acts as a parent, now we use the &lt;code&gt;has&lt;/code&gt;  pseudo-class to find a checked input  under the body element we then target the &lt;code&gt;p&lt;/code&gt;  element to hide it when the checkbox is checked and unhide it when we uncheck the checkbox.&lt;/p&gt;

&lt;p&gt;When combined with pseudo-elements like &lt;code&gt;::before&lt;/code&gt;  and &lt;code&gt;::after&lt;/code&gt;  you can make it more interactive as I did in the example, and of course, you can do more than just toggling things on and off.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS nesting
&lt;/h2&gt;

&lt;p&gt;Before if we were to style a specific descendant element of a given parent, we would have to prefix the parent or wrapping element before the element we want to style, let’s take an example of our previous example we want to style the &lt;code&gt;p&lt;/code&gt;  element we would have written a CSS that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;.9rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&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;p&gt;But do you know that we can do the same thing by nesting the &lt;code&gt;p&lt;/code&gt;  element inside the body declaration like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;box-sizing&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;border-box&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.9rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="err"&gt;p&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  nesting for pseudo-elements and pseudo-classes
&lt;/h3&gt;

&lt;p&gt;And we if we wanted to nest any pseudo-element or pseudo-class, we use the &lt;code&gt;&amp;amp;&lt;/code&gt;  for example we can simplify this input declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;
      &lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="nd"&gt;:has&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;appearance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;"Hide"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.4rem&lt;/span&gt; &lt;span class="m"&gt;1.4rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.4rem&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;p&gt;By nesting the &lt;code&gt;::before&lt;/code&gt;  selection inside the &lt;code&gt;input&lt;/code&gt;  selector like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;appearance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="err"&gt;&amp;amp;::before{&lt;/span&gt;
          &lt;span class="nl"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;"Hide"&lt;/span&gt;
           &lt;span class="n"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.4rem&lt;/span&gt; &lt;span class="m"&gt;1.4rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.4rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is helpful when you have long list of pseudo-elements and classes you want to apply to a single element you can nest them as the example we have to avoid having many definitions of the same element declaration.&lt;/p&gt;

&lt;p&gt;But be careful using this type of nesting because it can lead to unreadable CSS it's better to use it for pseudo-classes, pseudo-elements or when you are constantly prefixing your parent element on its descendant's children you want to style.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/scope"&gt;@scope&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Did you actually know that with CSS you can style elements based in a scope they are define in and you can limit the elements of the scope that gets affected, just as in normal programming when have scopes and we can limit the availability of the variable based on the scope the same with CSS.&lt;/p&gt;

&lt;p&gt;Let’s take an example to see how this would work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt; &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;section&amp;gt;&lt;/span&gt;

      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"profile"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./assets/people.png"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"article image"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"200"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"img-2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./assets/people.png"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"article image"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"200"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s say we have this HTML structure; we have a section that has two &lt;code&gt;divs&lt;/code&gt;  one of them has the class called &lt;code&gt;img-2&lt;/code&gt;  which has the same content as the first one they both have images take the first div.’s image and make it a rounded image much more like a profile, the snippet could look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.profile&lt;/span&gt; &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&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;p&gt;We can use &lt;a class="mentioned-user" href="https://dev.to/scope"&gt;@scope&lt;/a&gt; to limit the styles to only apply to the first image using the section we can write like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@scope&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;section&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;(.&lt;/span&gt;&lt;span class="n"&gt;img-2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;greenyellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we are essentially doing is we are taking &lt;code&gt;section&lt;/code&gt;  scope and target all the images elements within the &lt;code&gt;section&lt;/code&gt;  scope, &lt;code&gt;to&lt;/code&gt;  method is essential because it tells CSS to apply styles to every &lt;code&gt;img&lt;/code&gt; in the scope except the images that are found inside an element with the class of &lt;code&gt;.img-2&lt;/code&gt;  if you forgot to add it will add styles to all &lt;code&gt;img&lt;/code&gt; elements in the scope &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Rule of Thumb&lt;/p&gt;

&lt;p&gt;use &lt;code&gt;to&lt;/code&gt; to limit you’re styles to a specific scope only by excluding a given markup or scope you define inside it’s curry braces &lt;code&gt;()&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&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%2F3jg9ce6l0t3hv2pn01p5.gif" 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%2F3jg9ce6l0t3hv2pn01p5.gif" alt=" " width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  @container queries
&lt;/h2&gt;

&lt;p&gt;Container queries are another type of making responsive design but in a more a customized way, before the responsive required you to target the whole screen’s viewport or width to start applying styles based on a certain viewport the screen reaches, which sometimes was hard to work when you only wanted to add responsive to a certain section of your page.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;@container&lt;/code&gt; queries, you can add responsive designs based on the size of the parent element or the wrapping element.&lt;/p&gt;

&lt;p&gt;Consider this example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"blog"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;figure&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./assets/people.png"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"article image"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;figcaption&amp;gt;&lt;/span&gt;sample image&lt;span class="nt"&gt;&amp;lt;/figcaption&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"description"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          Lorem ipsum dolor sit amet consectetur adipisicing elit. Vitae,
          asperiores dolorum. Aspernatur laboriosam quam, nulla ipsum cumque
          dolores pariatur recusandae nobis ea itaque commodi similique eos
          dolor! Quae dolore eaque numquam quidem voluptatem, asperiores ea!
          Esse ut ratione quam quisquam.
        &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/figure&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have a section with a class of &lt;code&gt;blog&lt;/code&gt;  and we want to make the content display &lt;code&gt;flex&lt;/code&gt;  when its size in width is less than &lt;code&gt;640px&lt;/code&gt; , using container queries we can do it using the below example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;section&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="py"&gt;container-type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@container&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;640px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nt"&gt;figure&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;align-items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike &lt;code&gt;media&lt;/code&gt;  queries to be able to use container queries you first need to have an element wrapping the elements you want to have container queries on, and then target the wrapper element in our case we have &lt;code&gt;section&lt;/code&gt;  as our wrapper element and then use the property of &lt;code&gt;container-type&lt;/code&gt;  to specify if you want your styles to be triggered while &lt;strong&gt;scaling the screen&lt;/strong&gt; horizontally or scaling vertically, &lt;/p&gt;

&lt;p&gt;Most of the time you will want the responsive the occur when scaling horizontally meaning when the screen is shrinking on the x-axis for this behavior you use the &lt;strong&gt;&lt;code&gt;inline-size&lt;/code&gt;.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;If you want to apply vertically meaning shrinking on the y-axis you can use the &lt;strong&gt;&lt;code&gt;block-size&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And also, there is another one &lt;code&gt;both&lt;/code&gt;  to track both sides.&lt;/p&gt;

&lt;p&gt;Below there is a demo showing how container queries differ from me&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%2F3lnrcivvpni6v5xvampo.gif" 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%2F3lnrcivvpni6v5xvampo.gif" alt=" " width="720" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the demo, we have a black background color that is applying when our screen is below the width &lt;code&gt;640px&lt;/code&gt; and we have a green background that is later appearing when our container size with is below &lt;code&gt;640px&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can see the difference even though it is small, but the green background lasts a little longer than the black one because our container width is still less than 640px even though our screen width is greater than &lt;code&gt;640px&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;We have covered the &lt;code&gt;has&lt;/code&gt; pseudo-class which can be used style the parent element or style its descendants based on the descendant's behavior, style or attributes&lt;/p&gt;

&lt;p&gt;We’ve covered &lt;code&gt;@scope&lt;/code&gt; which we can use to only apply styles to a specific element in a scope of the markup structure and the &lt;code&gt;@container&lt;/code&gt; queries that we use to add responsive design based on the parent or container element size.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;This was few in more features that are being released in CSS and they can do more advanced things more than what I demoed in this guide, if you explore more you will find many use-cases for each feature and also the examples in this guide are also a good reference for more use-cases you may think of or challenge they can help solve with your CSS, Thanks for reading I hope you’ve enjoyed it.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>css</category>
      <category>design</category>
      <category>simplicity</category>
    </item>
    <item>
      <title>Docker key terms simplified</title>
      <dc:creator>jabo Landry </dc:creator>
      <pubDate>Fri, 06 Mar 2026 16:20:52 +0000</pubDate>
      <link>https://forem.com/jaboarnoldlandry/docker-key-terms-simplified-31c6</link>
      <guid>https://forem.com/jaboarnoldlandry/docker-key-terms-simplified-31c6</guid>
      <description>&lt;p&gt;Containerization is a must-have skill for DevOps engineers and software developers alike. It is essential for creating robust, deployable applications. In this article, I will explain the Docker concepts every developer needs to know and provide a hands-on example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;What is Docker?&lt;/li&gt;
&lt;li&gt;Why is Docker Important&lt;/li&gt;
&lt;li&gt;Idea of How Docker Works&lt;/li&gt;
&lt;li&gt;What is a Docker Image?&lt;/li&gt;
&lt;li&gt;Dockerfile Commands Explanation&lt;/li&gt;
&lt;li&gt;Running the Image&lt;/li&gt;
&lt;li&gt;What is a Docker Container?&lt;/li&gt;
&lt;li&gt;How to Run a Docker Container&lt;/li&gt;
&lt;li&gt;Docker Volumes&lt;/li&gt;
&lt;li&gt;Final Thoughts&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;to follow along this article be sure to &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download and install Docker Desktop.&lt;/li&gt;
&lt;li&gt;Ensure Docker is running (especially on macOS or Windows) while practicing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is docker:
&lt;/h2&gt;

&lt;p&gt;Docker is a software platform that helps package and run applications on any machine with the exact environment you have configured.&lt;/p&gt;

&lt;p&gt;Teams often face the "it works on my machine" issue, where an application runs locally but crashes when a teammate tries to run it. This happens due to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mismatched environment or library versions.&lt;/li&gt;
&lt;li&gt;Differences in Operating Systems (OS) that don't support specific features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Configuring every environment to match can be time-consuming and slows down development. Docker solves this by "containerizing" the environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is docker important:
&lt;/h3&gt;

&lt;p&gt;As a developer, you shouldn't waste time troubleshooting environment setups. Docker allows you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure the environment once and reuse it throughout the development process.&lt;/li&gt;
&lt;li&gt;Run multiple copies of the same application with different environments without conflicts.&lt;/li&gt;
&lt;li&gt;Eliminate library version mismatches.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Idea of how docker works:
&lt;/h4&gt;

&lt;p&gt;Imagine you have a minivan that you use as a home, a business venture, and transport all at once. You can drive anywhere, and everything you need is already inside. You don't need to find a new bed or kitchen every time you move; it's all in the van.&lt;/p&gt;

&lt;p&gt;That is the idea of Docker. It contains all the essentials needed to run an application in a single container. This container can be used by anyone, anywhere, without further configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a docker image:
&lt;/h2&gt;

&lt;p&gt;Docker image is a blueprint that describes how our application environment will look like and how it will be built from the ground up and after it is created it cannot be modified.&lt;/p&gt;

&lt;p&gt;with docker image we describe how docker should allocate and arrange our application environment this process happens in a file called &lt;code&gt;Dockerfile&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Dockerfile&lt;/code&gt; defines the layers (stages) required to run the container, starting from the OS up to your codebase.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;#this creates a node runtime of version 18 and OS of alpine&lt;/span&gt;
&lt;span class="k"&gt;From&lt;/span&gt;&lt;span class="s"&gt; node:18-alpine&lt;/span&gt;

&lt;span class="c"&gt;#this specifies the path where docker will store you application codes this could be any name of path you want&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app &lt;/span&gt;

&lt;span class="c"&gt;#copies the package file to docker files directory so that when npm can have packages to install&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package.json &lt;/span&gt;

&lt;span class="c"&gt;#this will run and install all the packages you are using in your package file&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; 

&lt;span class="c"&gt;#copies all the codebase in your directory to the docker files&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . . &lt;/span&gt;

&lt;span class="c"&gt;#define the port that will be used when testing the application&lt;/span&gt;

&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 3000&lt;/span&gt;

&lt;span class="c"&gt;# if you are using nodemon you write a command to start your application like this&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["npm", "run", "dev"]&lt;/span&gt;

&lt;span class="c"&gt;# if you are using node to execute the code you use&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["node", "name of the file.js"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Docker file commands Explanation
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;From&lt;/code&gt;  specifies which kind of runtime you are using in my case I am using JavaScript, but you can use whatever based on your project setup and the colon helps in specifying the version and the hyphen to specify the OS system the system will be built on top of. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;WORKDIR&lt;/code&gt;  this command specifies the directory in which docker will store your application codes generally you can specify any path you want but &lt;code&gt;/app&lt;/code&gt;  is the convenient one.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;COPY&lt;/code&gt;  this command copies a specific file from your project files to docker&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EXPOSE&lt;/code&gt;  this command specifies the port in which you are application is running on and also helps to define a port your application will run on when starting a container&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CMD&lt;/code&gt;  this command is the one that runs you application after it have been initialized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; for window users and macOS when running your application using &lt;code&gt;nodemon&lt;/code&gt;  package inside your run script file in &lt;code&gt;package.json&lt;/code&gt;  file place &lt;code&gt;-L&lt;/code&gt;  flag before your file for it to run, without it your application won’t get started unless you are a Linux user&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%2F87bdvc2sw2arj1uokhkv.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%2F87bdvc2sw2arj1uokhkv.png" alt=" " width="480" height="119"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Running the image
&lt;/h3&gt;

&lt;p&gt;After setting your docker file you can create an image by running the &lt;code&gt;build&lt;/code&gt;  command in your terminal using the &lt;code&gt;docker&lt;/code&gt;  command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; my-first-docker-image:version-one &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;build&lt;/code&gt;  command is the one used to build the image for your docker, and the &lt;code&gt;-t&lt;/code&gt; flag us used to specify the tag of the image.&lt;/p&gt;

&lt;p&gt;Image tag helps to differentiate images with the same name, docker image cannot be modified after they are built to edit it you would need to create a new image, but you may want to keep the same image but change its versions so you can track your changes and version over time and to do that the &lt;code&gt;-t&lt;/code&gt;  flag helps create a tag that you can refer as a version of the image, to specify the tag or version of your image you use double colon(&lt;code&gt;:&lt;/code&gt; )&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; my-first-docker-image:version-two &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you didn’t define the tag name by default docker will assign it the &lt;code&gt;latest&lt;/code&gt;  as the default tag name&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; my-first-docker-image &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;without &lt;code&gt;-t&lt;/code&gt;  flag it would hard to track down the changes of the image when we are constantly making changes and rebuilding the image&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.&lt;/code&gt;  tells docker where to look for files to copy&lt;/p&gt;

&lt;h2&gt;
  
  
  What is docker container:
&lt;/h2&gt;

&lt;p&gt;Docker container is the implementation of a plan created in docker image, now all of the specifications you’ve specified inside your &lt;code&gt;Dockerfile&lt;/code&gt; will be executed one by one and create a container of your application codes and also create the underlying OS which is &lt;code&gt;alpine&lt;/code&gt;  and a runtime environment of &lt;code&gt;node.js&lt;/code&gt;  below is the command to run and create a container.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to run a docker container
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;--name&lt;/span&gt; container_name &lt;span class="nt"&gt;-p&lt;/span&gt;  image-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Let’s start with knowing each command step by step&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;run&lt;/code&gt;  command is used to start a new container.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--name&lt;/code&gt; flag specifies the name for your container, the name you would like your container to have&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-p&lt;/code&gt;  flag is referred as port mapping this should be used if you have specified &lt;code&gt;EXPOSE&lt;/code&gt;  command in your &lt;code&gt;Dockerfile&lt;/code&gt;  it helps map any port you specify to the exposed port in docker file you put you preferred port on the left and the exposed port on the right and separate them using a colon.&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nt"&gt;-p&lt;/span&gt; prefferedPort:exposedPort
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;image-name&lt;/code&gt;  and then you have to specify the name of image in which you want to create a container from.&lt;/p&gt;

&lt;p&gt;running a container from the image we’ve created above we can something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;--name&lt;/span&gt; container_one &lt;span class="nt"&gt;-p&lt;/span&gt; 3000:3000 my-first-docker-image:version-one
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Docker Volumes
&lt;/h2&gt;

&lt;p&gt;Docker images are "read-only." If you change your code on your computer, the running container won't see those changes unless you rebuild the image. Docker Volumes solve this.&lt;/p&gt;

&lt;p&gt;Volumes act as a bridge between your local development environment and the running container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;--name&lt;/span&gt; container_one &lt;span class="nt"&gt;-p&lt;/span&gt; 3000:3000 &lt;span class="nt"&gt;-v&lt;/span&gt; /our apps location:./our docker specified path
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the command is the one we’ve been using to create a container before but The -v flag binds your local folder to the folder inside the container (e.g., /app). Now, any changes you make in your code editor will instantly reflect inside the running container!&lt;/p&gt;

&lt;p&gt;So, the real command can look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;--name&lt;/span&gt; container_one &lt;span class="nt"&gt;-p&lt;/span&gt; 3000:3000 &lt;span class="nt"&gt;-v&lt;/span&gt; /C:&lt;span class="se"&gt;\U&lt;/span&gt;sers&lt;span class="se"&gt;\a&lt;/span&gt;rnol&lt;span class="se"&gt;\O&lt;/span&gt;neDrive&lt;span class="se"&gt;\D&lt;/span&gt;ocumentos&lt;span class="se"&gt;\n&lt;/span&gt;ewContent&lt;span class="se"&gt;\c&lt;/span&gt;lient:./app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Docker makes development faster and smoother. Whether you are aiming to be a DevOps engineer or a Full-stack developer, understanding these basics is essential for modern software engineering.&lt;/p&gt;

&lt;p&gt;Thank you for reading! If you learned something new or have suggestions, please let me know in the comments below.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>cloud</category>
      <category>cloudcomputing</category>
      <category>docker</category>
    </item>
    <item>
      <title>Procrastination in disguise</title>
      <dc:creator>jabo Landry </dc:creator>
      <pubDate>Sat, 21 Feb 2026 18:18:25 +0000</pubDate>
      <link>https://forem.com/jaboarnoldlandry/procrastination-in-disguise-3605</link>
      <guid>https://forem.com/jaboarnoldlandry/procrastination-in-disguise-3605</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Procrastination in disguise&lt;/li&gt;
&lt;li&gt;Stepping out of your comfort zone&lt;/li&gt;
&lt;li&gt;Final Thoughts&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Recently, this month, I have started exploring TypeScript. It was very exciting at the start because I was gaining new experience and skills under my sleeves, like everyone else who is doing something for the first time. Little by little, you start feeling overwhelmed and confused, not knowing when to use a certain concept over another.&lt;/p&gt;

&lt;p&gt;This kind of feeling of uncertainty is a good sign that you are learning, you are facing new challenges beyond what you used to, and you are stepping out of your comfort zone which is a good sign but if you continue to hold on to those feelings and not do the right thing which is practice it may later lead to procrastination.&lt;/p&gt;

&lt;h1&gt;
  
  
  Procrastination in disguise
&lt;/h1&gt;

&lt;p&gt;Procrastinating is a thing we all programmers in our case try to avoid at all costs, so we tend to do more reading and more research on a certain concept.&lt;/p&gt;

&lt;p&gt;I have come to realization that reading in programming is another type of procrastination beyond the saying “I will do this tomorrow”.&lt;/p&gt;

&lt;p&gt;a typical example is when I was reading about &lt;code&gt;interfaces&lt;/code&gt; and &lt;code&gt;type aliases&lt;/code&gt;  at first, I had two confusions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the difference between type aliases and interface when creating types&lt;/li&gt;
&lt;li&gt;Why do we have them doing the exact thing which is creating type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I keep on reading about interfaces and type aliases but within me deep down I felt a fear to embrace them and practice on them to decide on where we might need to use one over the other, in which case and under which circumstances. &lt;/p&gt;

&lt;p&gt;And I ignored that feeling and kept on reading to make myself believe I am learning something, but it didn’t click, even though I read about 5 articles and maybe watched a bunch of videos on YouTube.&lt;/p&gt;

&lt;h1&gt;
  
  
  Stepping out of your comfort zone
&lt;/h1&gt;

&lt;p&gt;To solve the confusion about the two concepts I confronted myself and I made a conclusion to build a simple Dictionary app that you input a word and fetches data from an oxford dictionary public API for the meaning and other ways we can pronounce the word you’ve searched.&lt;/p&gt;

&lt;p&gt;In the first attempt to fetch and work with data I had that aha moment where I started knowing why we need &lt;code&gt;type aliases&lt;/code&gt; and &lt;code&gt;interface&lt;/code&gt; how to use them effectively for clarity. I’ve found out that &lt;code&gt;interfaces&lt;/code&gt; are useful when you want to create a structure of the object and match your frontend with the incoming API response.&lt;/p&gt;

&lt;p&gt;When creating those interfaces, I came across an issue of using the same type on every field of the interface I am creating I noticed that they have the same type  instead  of always typing the same type I can simplify it by creating a new name for a certain type that is repeating multiple times and use that name to assign type to my field and that was achieved with the help of &lt;code&gt;type aliases&lt;/code&gt;  which makes the process of extending and changing types in future fast and easy to do.&lt;/p&gt;

&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Through this process I have found that sometimes we developers we tend to learn a certain concept by reading it believing we are understanding it in detail, and we will then practice later on when we have conceptually grasped the concept.&lt;/p&gt;

&lt;p&gt;But programming is a fascinating concept, and the more you read the more you fear practicing and with no practice there is no progress&lt;/p&gt;

&lt;p&gt;Ending this, I would urge all of you guys on any level, whatever concept you are learning, to have an introduction and then jump into practice, and after you hit a wall, you can go back to read more about the concept. That way, you will not be procrastinating, but you will be advancing and building depth more.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>motivation</category>
      <category>webdev</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Enhancement on Caching and Service Workers</title>
      <dc:creator>jabo Landry </dc:creator>
      <pubDate>Sat, 07 Feb 2026 16:53:39 +0000</pubDate>
      <link>https://forem.com/jaboarnoldlandry/enhancement-on-caching-and-service-workers-1bfl</link>
      <guid>https://forem.com/jaboarnoldlandry/enhancement-on-caching-and-service-workers-1bfl</guid>
      <description>&lt;h1&gt;
  
  
  Welcome to My Video Post
&lt;/h1&gt;

&lt;p&gt;Following my &lt;a href="https://dev.to/jaboarnoldlandry/comprehensive-guide-on-getting-started-with-caching-using-service-workers-11b1"&gt;previous article on Comprehensive guide on getting started with caching using service workers&lt;/a&gt;,&lt;br&gt;&lt;br&gt;
I’ve created a video explaining how to update an already existing cache.  &lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

&lt;p&gt;

  &lt;iframe src="https://www.youtube.com/embed/Owe4qab_EVE"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;




&lt;h2&gt;
  
  
  The Video covers
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;practical steps for updating cached assets and resources.
&lt;/li&gt;
&lt;li&gt;It also highlights common pitfalls and how to avoid them.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Thanks for watching!&lt;br&gt;&lt;br&gt;
If you’ve tried similar caching strategies, share your experience in the comments—I’d love to compare notes.&lt;/p&gt;

</description>
      <category>video</category>
      <category>javascript</category>
      <category>cache</category>
      <category>performance</category>
    </item>
    <item>
      <title>Comprehensive guide on getting started with caching using service workers</title>
      <dc:creator>jabo Landry </dc:creator>
      <pubDate>Mon, 26 Jan 2026 06:43:46 +0000</pubDate>
      <link>https://forem.com/jaboarnoldlandry/comprehensive-guide-on-getting-started-with-caching-using-service-workers-11b1</link>
      <guid>https://forem.com/jaboarnoldlandry/comprehensive-guide-on-getting-started-with-caching-using-service-workers-11b1</guid>
      <description>&lt;p&gt;Ever wondered why some sites loads slow when you first visit them, but after loading the full page when you get back the next time the site loads instantly fast, in today’s guide I will be  walking you through the beauty of service worker by demonstrating how you can reduce you’re site loading time in the users browsing device the next time they visit you’re site.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Prerequisites&lt;/li&gt;
&lt;li&gt;What is a service Worker&lt;/li&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;Basic caching implementation using Service worker&lt;/li&gt;
&lt;li&gt;
Starting Cache operations

&lt;ul&gt;
&lt;li&gt;install event&lt;/li&gt;
&lt;li&gt;activate event&lt;/li&gt;
&lt;li&gt;Fetch event&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Wrap Up&lt;/li&gt;

&lt;li&gt;Final Thoughts&lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  Prerequisites:
&lt;/h1&gt;

&lt;p&gt;To follow along this guide, you must be familiar with the following JavaScript concept:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promises&lt;/li&gt;
&lt;li&gt;Events in JavaScript&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What is a service Worker:
&lt;/h1&gt;

&lt;p&gt;service workers is a JavaScript technique of running tasks in the background from the page main thread to help us developers, intercept request, cache resources from the client and even allows the app to run even offline, we will be focusing on how to cache resources/assets of the site in this article today.&lt;/p&gt;

&lt;h1&gt;
  
  
  Caching
&lt;/h1&gt;

&lt;p&gt;caching is to save resources and assets of the site that takes too much time to load to the browser’s memory so the next time we try to access them we won’t need to re-download them from the server rather but on the browser's memory which is instantly first than getting them from the server.&lt;/p&gt;

&lt;h1&gt;
  
  
  Basic caching implementation using Service worker
&lt;/h1&gt;

&lt;p&gt;I have a created a simple site of space tourism which contains information about different location on the space you can visit in the space and the details about it; is a frontend mentor challenge which you can find using this &lt;a href="https://www.frontendmentor.io/challenges/space-tourism-multipage-website-gRWj1URZ3" rel="noopener noreferrer"&gt;link&lt;/a&gt; if you want to try it out and follow along with.&lt;/p&gt;

&lt;p&gt;this project is the perfect scenario to explain how we can cache the resources to reduce the load time on our site, and to be clear a site with a lot of visuals like images can be slow so I want to cache the home page to show how you can cache the site if you find site loading slowly and give you an insight on how service worker and caching works.&lt;/p&gt;

&lt;p&gt;Looking at this image below, I am displaying all the time and size each file is taking to load the full homepage or index page&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%2Fw10ayidfwql8v9o57o5e.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%2Fw10ayidfwql8v9o57o5e.png" alt=" " width="800" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Image showing the network status when I first load the site on my device for the first time&lt;/p&gt;

&lt;p&gt;You can see that I am fetching files with 264 KB in 266ms as the time it takes to load the whole landing page which means when I again visit the site it will take the same amount of time and size to load which is not ideal, again you find this information by going to the &lt;strong&gt;devtools→network&lt;/strong&gt; tab&lt;/p&gt;

&lt;p&gt;We start by registering a service worker in the app entry or main file mine is &lt;code&gt;index.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;serviceWorker&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serviceWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/sw.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;registeredSw&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;registeredSw&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;err&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;p&gt;We start by checking if the current browser the app/site running on supports the service Worker, &lt;code&gt;serviceWorker&lt;/code&gt; is a property of the &lt;strong&gt;navigator object of the windows object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;we use the &lt;code&gt;register()&lt;/code&gt;  on the &lt;code&gt;serviceWorker&lt;/code&gt; to register a service worker in the browser, which takes in 2 arguments the first one is the destination where we’ve created a file which we will be writing in all our caching logic it is advised to put the service worker file in global directory in order to have access to all files&lt;/p&gt;

&lt;p&gt;and the second argument is optional, &lt;strong&gt;the scope&lt;/strong&gt; of files the service worker will track if you’ve saved it in a certain folder, it’s scope would be that given directory that why it advisable to put in the global directory.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;register()&lt;/code&gt; returns a &lt;strong&gt;Promise&lt;/strong&gt; we use &lt;code&gt;then&lt;/code&gt; to display the registered service worker and &lt;code&gt;catch&lt;/code&gt; to catch any errors that may occur while registering the service worker&lt;/p&gt;

&lt;p&gt;if it is successfully registered will be returned its instance in &lt;code&gt;console&lt;/code&gt; which will look like this&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%2Fpf234vrtqgbzqceyhgwj.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%2Fpf234vrtqgbzqceyhgwj.png" alt=" " width="800" height="291"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The console shows the registered service worker&lt;/p&gt;

&lt;p&gt;To view the created service worker, you will need to navigate to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;devtools&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;application and choose the &lt;strong&gt;Service workers&lt;/strong&gt; pane on the left panel of the application section&lt;/li&gt;
&lt;/ul&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%2F79wd52pze7gun7j3wh0e.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%2F79wd52pze7gun7j3wh0e.png" alt=" " width="800" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;service worker takes 3 phases it passes through to be installed which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;installation&lt;/strong&gt;: which have completed in the first step when we register a new service worker&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Activation&lt;/strong&gt;: activation of the service worker to start operating&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;wait&lt;/strong&gt;:  Still waiting for the service to be activating&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Starting Cache operations:
&lt;/h2&gt;

&lt;p&gt;In service Workers we use event driven approach to cache and fetch our resources and assets; in this section we will look each one of events needed to start service worker and cache your assets and resources and explain its part in caching resources/assets.&lt;/p&gt;

&lt;h3&gt;
  
  
  install event:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Exploration of the event&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;this is the first event to be fired, remember when we registered a new service worker that’s when the event is triggered&lt;/p&gt;

&lt;p&gt;in this event we create a cache storage in the browser and add the resources that we will need to be cached in the cache.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation of the event&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;to implement switch to the &lt;code&gt;sw.js&lt;/code&gt; you have registered as you're service worker file and then implement the following snippet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;resourcesToCache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&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="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/index.html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/src/JavaScript/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/src/output.css&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cacheName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;version-1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;self&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="s2"&gt;install&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="nx"&gt;event&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;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;waitUntil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;caches&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cacheName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;cache&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;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resourcesToCache&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="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&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;err&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above snippet declare  &lt;code&gt;resourcesToCache&lt;/code&gt; array to keep track of multiple resources and assets we want to cache and &lt;code&gt;cacheName&lt;/code&gt;  is the name that will use as the cache name.&lt;/p&gt;

&lt;h3&gt;
  
  
  event setup:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;the &lt;code&gt;self&lt;/code&gt; is service worker we have registered, essentially it is attaching event to itself that’s why they call it &lt;code&gt;self&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;we attach an event of &lt;code&gt;install&lt;/code&gt;  as well as a callback function that will trigger when service worker is first registered on any device browser our site/app is running in.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Cache setup:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;waitUntil()&lt;/code&gt;  on the install event ensures that the service worker is installed after the operation inside the &lt;code&gt;waitUntil()&lt;/code&gt; has completed.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;caches&lt;/code&gt;  object helps create, manage the browser’s cache storage.&lt;/li&gt;
&lt;li&gt;We start by opening a new cache with &lt;code&gt;open()&lt;/code&gt;  which takes a string as an argument that will act as a cache name, we used the &lt;code&gt;cacheName&lt;/code&gt;  variable.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;open()&lt;/code&gt; returns a promise we chain &lt;code&gt;then()&lt;/code&gt;  to access the created cache.&lt;/li&gt;
&lt;li&gt;The  &lt;code&gt;.addAll()&lt;/code&gt; to add multiple resources we would like to cache and finally a &lt;code&gt;catch&lt;/code&gt;  block to handle any errors that may occur in caching.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;you’ve just cached you’re first assets/resources in your browser cache, to view the changes go to &lt;strong&gt;devtools(application→Service workers)&lt;/strong&gt; and you should see you a button labeled as &lt;code&gt;skipWaiting&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%2Frxvyh4f12nfpgpsa3uud.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%2Frxvyh4f12nfpgpsa3uud.png" alt=" " width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Service worker panel in the browser&lt;/p&gt;

&lt;p&gt;click on &lt;code&gt;skipWaiting&lt;/code&gt; and then look on the left and then expand to &lt;code&gt;Cache Storage&lt;/code&gt; tab you should see the name of you cache and when you click on it you will see resources cached insider there&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%2F70onuwi141w3f4snergk.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%2F70onuwi141w3f4snergk.png" alt=" " width="800" height="171"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;created cache in the browser&lt;/p&gt;

&lt;p&gt;and this should be it for setting up the cache for the site&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; that to make testing easier it is better to enable the checkbox for &lt;code&gt;Update on reload&lt;/code&gt;  in service workers’ section; to test without needing to click on &lt;code&gt;skipWaiting&lt;/code&gt; every single time you make changes to the page.&lt;/p&gt;




&lt;h3&gt;
  
  
  activate event:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Exploration of the event&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;this event fires every time there is a new change in a service worker file, its responsible is to activate a service worker.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation of the event&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nb"&gt;self&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="s2"&gt;activate&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="nx"&gt;event&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Activated service worker&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we use the &lt;code&gt;activate&lt;/code&gt;  event to trigger &lt;strong&gt;activation of our service worker&lt;/strong&gt; every time there is a change in service worker file, so the next time you change something from the &lt;code&gt;sw.js&lt;/code&gt; the &lt;code&gt;activate&lt;/code&gt; event will be triggered and in console you will see the message ‘&lt;strong&gt;Activated service worker&lt;/strong&gt;’.&lt;/p&gt;




&lt;h3&gt;
  
  
  Fetch event:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Exploration of the event:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This event is fired every time you make a request to the server requesting resources/assets, and this is where we intercept the request and first check if the data we are trying to download from the server includes in our cache if we have it in our cache we can directly return them if not we can continue with the request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation of the event:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nb"&gt;self&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="s2"&gt;fetch&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="nx"&gt;event&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;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;respondWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;caches&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;request&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in above snippet we have:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;attached &lt;code&gt;fetch&lt;/code&gt; event to service worker that will trigger any time we make a request&lt;/li&gt;
&lt;li&gt;we access the &lt;code&gt;respondWith()&lt;/code&gt; of the &lt;code&gt;fetch&lt;/code&gt;  event, the &lt;code&gt;respondWith()&lt;/code&gt; method will help intercept the request before it sends to the server requesting some assets/ resources&lt;/li&gt;
&lt;li&gt;we access the &lt;code&gt;request&lt;/code&gt; property on the &lt;code&gt;event&lt;/code&gt;  object.&lt;/li&gt;
&lt;li&gt;inside the &lt;code&gt;respondWith()&lt;/code&gt; we then use &lt;code&gt;match()&lt;/code&gt; and pass our request to find the matching resources/assets in cache similar to the request we are sending to the server.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;caches.match()&lt;/code&gt; returns a promise, we attach &lt;code&gt;then()&lt;/code&gt; to handle the resolve the promise and if it has found some assets/resources in our cache we then return them instantly if not we continue with the request.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;to see the effects of this let’s compare the network of the first image when we had no service worker and now, when we have a service worker&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure A:&lt;/strong&gt; Before service worker implementation network report&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%2Fjyq16woj0r988yry62p9.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%2Fjyq16woj0r988yry62p9.png" alt=" " width="800" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;network request report of site with not service worker and cache&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure B:&lt;/strong&gt; After service worker implementation network report&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%2Ffj8lqmgev2tibgcjnyub.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%2Ffj8lqmgev2tibgcjnyub.png" alt=" " width="800" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;network request report of site with service worker and cache&lt;/p&gt;

&lt;p&gt;With this you can see the difference, the file that we are getting from our service worker have a low load time compared to before the service worker and you can see they are taking up 0 size compared to before.&lt;/p&gt;

&lt;p&gt;before we had a load time of 266 and after service worker, we have a load time of 124 which is less than half of the first load time we had before we cache.&lt;/p&gt;

&lt;p&gt;we also had &lt;strong&gt;264Kb&lt;/strong&gt; of our resource size before we apply service worker but now, we only have &lt;strong&gt;116Kb&lt;/strong&gt; of resource size.&lt;/p&gt;

&lt;h1&gt;
  
  
  Wrap Up
&lt;/h1&gt;

&lt;p&gt;In this guide we covered how to apply cache to static resources that may cause long load time which can be a bad user experience on the site, &lt;/p&gt;

&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Service workers are not only limited to caching resources they can be used for push notifications, enabling site accessibility in offline mode which is commonly known as PWA (Progress web app)&lt;/p&gt;

&lt;p&gt;This guide helps you get started working with service worker, basic implementation and fundamental explanation of how caching works, in future articles I am planning to dive deep of how we can use service worker not only for caching but also for offline accessibility and background sync techniques.&lt;/p&gt;

</description>
      <category>performance</category>
      <category>pwa</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding Stateful Functions Through JavaScript Closures</title>
      <dc:creator>jabo Landry </dc:creator>
      <pubDate>Mon, 12 Jan 2026 10:15:40 +0000</pubDate>
      <link>https://forem.com/jaboarnoldlandry/understanding-stateful-functions-through-javascript-closures-1f97</link>
      <guid>https://forem.com/jaboarnoldlandry/understanding-stateful-functions-through-javascript-closures-1f97</guid>
      <description>&lt;p&gt;Closure In JavaScript is a function that retains access to variables from its outer scope, even after the outer function has finished executing.&lt;/p&gt;

&lt;p&gt;This definition may seem complicated at the first, but don’t worry in this article— we will Learn how JavaScript closures transform functions from stateless to stateful, with practical examples and the mental model that makes everything click.&lt;/p&gt;

&lt;p&gt;in this article we will cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Why We Need Closures &lt;/li&gt;
&lt;li&gt;
What is Lexical Scope &lt;/li&gt;
&lt;li&gt;
Understanding Closures and Lexical Scope &lt;/li&gt;
&lt;li&gt;
Working with Closure &lt;/li&gt;
&lt;li&gt;
Use Cases for Closures &lt;/li&gt;
&lt;li&gt;Final Remarks&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Why We need closures
&lt;/h1&gt;

&lt;p&gt;Closures are very important to know for you to be able to use today's modern frameworks like React, Redux library.&lt;/p&gt;

&lt;p&gt;In this guide we will go through the inner workings of closures and also provide you with use-cases and application of closures in React framework and in real world example using closures.&lt;/p&gt;

&lt;p&gt;Advantages of closures includes: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Helps keep the function pure without mutating the external of it.&lt;/li&gt;
&lt;li&gt;Helps make variable private the scope they are declare in.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What is Lexical scope:
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Lexical Scope by definition
&lt;/h2&gt;

&lt;p&gt;Lexical scope in JavaScript are rules that determines how and where the variable can be accessed in your program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lexical Scope under the hood
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;lexical scope&lt;/code&gt; under the hood is an &lt;code&gt;object&lt;/code&gt; in JavaScript’s scope either in a function or any JavaScript scope you may think of &lt;/p&gt;

&lt;p&gt;that object which we call &lt;code&gt;Lexical&lt;/code&gt; scope keeps track of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the variable in that scope.&lt;/li&gt;
&lt;li&gt;the value of &lt;code&gt;this&lt;/code&gt; of that scope.&lt;/li&gt;
&lt;li&gt;the pointer that points to the outer lexical scope if there any if there is no other scope above that the point is null&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;so, when we have a function that looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;test&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;4&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function lexical scope would look like this:&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%2F5syxqen8r6avj2m47bbk.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%2F5syxqen8r6avj2m47bbk.png" alt=" " width="653" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;as we demonstrated in this diagram each scope has its own lexical scope which points to the outer lexical scope above it.&lt;/p&gt;

&lt;p&gt;when in the &lt;code&gt;test()&lt;/code&gt; you try to access the variable &lt;code&gt;x&lt;/code&gt; JavaScript will look for it in the lexical scope of &lt;code&gt;test()&lt;/code&gt; if it is not there, it will then look in its outer lexical scope pointer which is the global one and if we have &lt;code&gt;x&lt;/code&gt;  there we will then access to &lt;code&gt;x&lt;/code&gt; and use it inside our function of &lt;code&gt;test()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What happens when we don’t have it, we will continue go up the tree and until we hit the lexical scope that’s points to null and that’s when you get the error saying: &lt;code&gt;Uncaught ReferenceError: x is not defined&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;test&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;4&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&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;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;//Uncaught ReferenceError: x is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding Closures and Lexical scope:
&lt;/h2&gt;

&lt;p&gt;To understand closures, we first need to understand how the normal function works under the hood. this will helps knowing what options with closure that are limited when using usual function.&lt;/p&gt;

&lt;p&gt;A normal function when its execution finishes there is a memory management mechanism in JavaScript that is known as &lt;code&gt;garbage collection&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Garbage collection:&lt;/strong&gt; is a mechanism for JavaScript to clean up the allocated memory for non-primitive data types when they are no longer in use, again if you don’t know how functions and non-primitive data types how are stored in memory, I have an article which talks about it which you can find &lt;a href="https://dev.to/jaboarnoldlandry/lets-discuss-deep-and-shallow-clone-in-javascript-3om7"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;garbage collection removes the allocated memory when the function is no longer in use makes the functions after execution reset all its variables and values declared with in its block scope of a function or the passed argument to the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;test&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;4&lt;/span&gt;
    &lt;span class="k"&gt;return&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;1&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;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 5&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;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 5&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;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 5&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;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No matter how long we call the function the result will always be the same unless we have variable  &lt;code&gt;y&lt;/code&gt; in global scope&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;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;test&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;y&lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&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;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 5&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;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 6&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;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 7&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;test&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens here is the garbage collection will clear out any allocated space that is no longer has reference with global object in case of browser the global object is the &lt;code&gt;window&lt;/code&gt; object, When the function finished executing it cuts reference and ties to the window object which then makes it eligible for garbage collected and wipe out all of its declared variable and passed argument if there are any.&lt;/p&gt;

&lt;p&gt;But when we have declared a variable in a global scope, the declared variable connection with the &lt;code&gt;window&lt;/code&gt; object will stay forever until you remove the variable from the global scope, which is why the variable is updated to a new value every time we call the function, because the variable state is updating.&lt;/p&gt;

&lt;p&gt;To give you a better mental model shift around these concepts you should think of them like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;stateless function:&lt;/strong&gt; a function that has no access to variable(s) outside of its scope the function variables are &lt;strong&gt;stateless&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;stateful function:&lt;/strong&gt; the function with access variable(s) from outside of its scope they have state which can be tracked and updated over time which makes them persists data and update as we go.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Working with closure:
&lt;/h1&gt;

&lt;p&gt;You know how the whole life cycle of execution of functions and a knowledge of how lexical scope works.&lt;/p&gt;

&lt;p&gt;I want you to take a look at this snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;counter&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;0&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="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;1&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;p&gt;this is the most common example you will see online of working with closures, and yeah this is how we create a closure we return a function and wrap it inside another function so when we call the outer function in our case &lt;code&gt;counter&lt;/code&gt; it will return  another function which will look like this &lt;code&gt;[Function (anonymous)]&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;because as we have seen in the example above the &lt;code&gt;counter&lt;/code&gt; returns a function, now the returned function has reference to its outer scope variable &lt;code&gt;n&lt;/code&gt; thus making connection with its lexical scope, since we have the connection with variable outside of the scope of the inner function it will persist the state of data.&lt;/p&gt;

&lt;p&gt;remember the stateful mental model** it states that &lt;strong&gt;“the function with access to variable(s) from outside of its scope they have state which can be tracked and updated over time which makes them persists data and update as we go.”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It means that since we have the reference to the variable &lt;code&gt;n&lt;/code&gt; garbage collection will not remove variable &lt;code&gt;n&lt;/code&gt; from the memory because there is some function that is referencing it,&lt;/p&gt;

&lt;p&gt;The outer function will be removed but &lt;code&gt;n&lt;/code&gt; will stay because of the connection it has with the inner function and that’s why mostly you will hear people define closure as &lt;strong&gt;“a function that retains its lexical scope even after its outer function has finished executing”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;now because of closures we’ve made variable &lt;code&gt;n&lt;/code&gt; from &lt;strong&gt;stateless&lt;/strong&gt; to &lt;strong&gt;state preservation&lt;/strong&gt; and now we can use the function to increment numbers we want, and here is how we execute the closure function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// assing the return function to a new variable&lt;/span&gt;
&lt;span class="c1"&gt;// use that variable to get the counts from the inner 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;count&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;count&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;//2&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;count&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;//3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Tricky Question
&lt;/h3&gt;

&lt;p&gt;Try and figure out what is a closure from the below 2 snippets &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outer&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;p&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="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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&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;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outer&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;results&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;results&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 2&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;results&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;B&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;(){&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;p&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&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;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outer&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;results&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;results&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;results&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well option &lt;strong&gt;A&lt;/strong&gt; is the real answer because it has reference to its outer scope while &lt;strong&gt;B&lt;/strong&gt; is returning another function true, &lt;/p&gt;

&lt;p&gt;but we are declaring variable within that function which means the moment the function finishes executing garbage collection will wipe out everything from &lt;strong&gt;function B&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;remember the stateless mental model: it states that &lt;strong&gt;“a function that has no access to variable(s) outside of its scope the function variables are stateless”&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Use Cases for Closures
&lt;/h1&gt;

&lt;p&gt;Let’s try to explore one practical use cases for closures that feels more real and makes sense to understand closure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example for use Case:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;suppose we are creating a function that lists the names of members of our app and the country they are representing; with the stateless way we can create our function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;introduceYou&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;country&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;`Hi I am &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="s2"&gt; and I am representing &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;country&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;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;introduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jabo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rwanda&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Jabo and I am respresenting Rwanda&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;introduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Arnold&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rwanda&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Arnold and I am respresenting Rwanda &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;introduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Landry&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rwanda&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Landry and I am respresenting Rwanda &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;introduceYou&lt;/span&gt;&lt;span class="p"&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rwanda&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am John and I am respresenting Rwanda &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;introduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Abdul&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UAE&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Abdul and I am respresenting UAE &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;introduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jane&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;USA&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Jane and I am respresenting USA &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;introduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Doe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;USA&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Doe and I am respresenting USA &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Problems in Changing the existing structure
&lt;/h3&gt;

&lt;p&gt;This approach works, but here’s a catch, you came up with the idea like you know what let instead use the city names instead of country names to change this would look very tedious say you have a thousand representing &lt;strong&gt;Rwanda,&lt;/strong&gt; and millions representing &lt;strong&gt;USA.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution for the change
&lt;/h3&gt;

&lt;p&gt;it would be hard to change all of those places and to be honest it is time consuming, but there is a solution , you can use closure to reference the thing that is common to the data we trying to capture in our case &lt;code&gt;country&lt;/code&gt; some users may have the same country or many of them; So we will make that a default reference that will stay in memory for as long as we wish to and this will make it easier changing it to something new like city&lt;/p&gt;

&lt;h3&gt;
  
  
  closure solution implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;introduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;locaction&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;user&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="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hi I am &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="s2"&gt; and I am representing &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;location&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="c1"&gt;//now we create a function that will handle people from different region&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;rwandaUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;introduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rwanda&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;americanUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;IntroduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;USA&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;uaeUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;IntroduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;UAE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//and now we create a function for users based on the variable country we created&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;rwandaUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jabo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Jabo and I am respresenting Rwanda&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;rwandaUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Arnold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Arnold and I am respresenting Rwanda&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;americanUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jane&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Jane and I am respresenting USA&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;uaeUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Abdul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Abdul and I am respresenting UAE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now if you want to change to their city all we need to be go where we’ve declared a certain user of a certain country and change the passed argument to be the city instead of country&lt;/p&gt;

&lt;p&gt;example (let’s change &lt;code&gt;rwandaUsers&lt;/code&gt; to use the city &lt;strong&gt;Kigali&lt;/strong&gt; instead)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;rwandaUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;introduceYou&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Kigali&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;rwandaUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jabo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Jabo and I am respresenting Kigali&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;rwandaUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Arnold&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Hi I am Arnold and I am respresenting Kigali&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Final Remarks
&lt;/h1&gt;

&lt;p&gt;When I was preparing this article, I found that React states are built using closures which links back to that mental model shift for &lt;strong&gt;stateless&lt;/strong&gt; and &lt;strong&gt;stateful function&lt;/strong&gt; I shared with you, those mental model helped me realize why React state are called &lt;code&gt;state&lt;/code&gt; and how they are able to persist data throughout component life cycle.&lt;/p&gt;

&lt;p&gt;I hope this article gave you both understanding and confidence with closures. If you found it valuable, please share it with someone who's struggling with this concept—it might be the explanation that finally makes it click for them.&lt;/p&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Beyond Server‑Side Rendering: Rediscovering PHP and the Joy of Experimenting</title>
      <dc:creator>jabo Landry </dc:creator>
      <pubDate>Wed, 31 Dec 2025 20:10:38 +0000</pubDate>
      <link>https://forem.com/jaboarnoldlandry/beyond-server-side-rendering-rediscovering-php-and-the-joy-of-experimenting-2oo6</link>
      <guid>https://forem.com/jaboarnoldlandry/beyond-server-side-rendering-rediscovering-php-and-the-joy-of-experimenting-2oo6</guid>
      <description>&lt;h2&gt;
  
  
  The Old Way We Were Taught
&lt;/h2&gt;

&lt;p&gt;For years we've been trained and taught that working with PHP you surely need to use the server-side rendering to complete the project, and it did stick in many of us that when we are developing PHP based projects we stick to that principle.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Turning Point
&lt;/h2&gt;

&lt;p&gt;And without considering looking for other ways to do things and they come a day I had some school assignment for developing a backend project using PHP.&lt;/p&gt;

&lt;p&gt;I hated PHP way of doing things using the server-side rendering and something crosses my mind, "wait is there a way can change the narrative and came up with something new hmm...🤔🤔".&lt;/p&gt;

&lt;h2&gt;
  
  
  Exploring New Possibilities
&lt;/h2&gt;

&lt;p&gt;And just like that I said let's explore if it is possible to create APIs in PHP as modern web application and use JavaScript to fetch those API data provided with our PHP backend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rediscovering PHP’s Strengths
&lt;/h2&gt;

&lt;p&gt;I went all in to find the ways to implement such thing and guess what I found it is actually 1000/100 possible, and this time I was feeling like I just invented a new rocket.&lt;/p&gt;

&lt;p&gt;And I was like all this time there was something great with PHP and we noticed we blindly hated on it and some of us have already buried and didn't even want to look on PHP again.&lt;/p&gt;

&lt;p&gt;PHP has a lot of features in it like &lt;strong&gt;Bearer tokens. for authentication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This solved a lot of problems that came with working server-side rendering of PHP like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Having backend errors showing up in frontend which for me I consider unprofessional&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Separation of concern which is a core in working with today's efficient systems&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Lesson Learned
&lt;/h2&gt;

&lt;p&gt;I used to not get the term they used to say, &lt;strong&gt;"There is no one way/solution to a problem in programming"&lt;/strong&gt; and at that time I finally released and felt what they meant when they mention that quote.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Advice to Developers
&lt;/h2&gt;

&lt;p&gt;My advice to all of you who have made it to end of the article is to not consider one way to a solution and to always be curious and try to apply what you've learned to unlock new patterns to make program fun because seriously if you do things in a certain defined way you will get bored of programming eventually.&lt;/p&gt;

&lt;p&gt;so go out there challenge yourselves find new ways to do things try combining new ways with old way to boast that dopamine of yours and fun in programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Well, that's all I got to share any suggestions for improvement? sure, I got you-just write down your thought and feedback in the comment section I do be happy to interact.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>php</category>
    </item>
    <item>
      <title>Let's discuss Deep and Shallow Clone in JavaScript</title>
      <dc:creator>jabo Landry </dc:creator>
      <pubDate>Sat, 01 Nov 2025 13:43:24 +0000</pubDate>
      <link>https://forem.com/jaboarnoldlandry/lets-discuss-deep-and-shallow-clone-in-javascript-3om7</link>
      <guid>https://forem.com/jaboarnoldlandry/lets-discuss-deep-and-shallow-clone-in-javascript-3om7</guid>
      <description>&lt;p&gt;I want to give you a test, so you get the real reason you need to learn about Deep and shallow clone or copy and why we still need to have the understanding of immutable and mutability in JavaScript ready let’s go 😎&lt;/p&gt;

&lt;p&gt;In previous article I discussed about mutability and immutability in JavaScript, if you’ve read the article kudus to you and if you haven’t, please check it out &lt;a href="https://lazizi.rw/mutability-and-immutability-in-javascript/" rel="noopener noreferrer"&gt;immutability and mutability in JavaScript&lt;/a&gt; because in this article I will be assuming you understand the concept of immutability and mutability in JavaScript and for those who have read it,&lt;/p&gt;

&lt;p&gt;can you figure the output of the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ['apple', 'mango', 'banana']
const fruitsAndVeg = ['pinapple', 'peaches',['dodo', 'tomato' ]]
const fruitsCopy = [...fruits];
const fruitsAndVegCopy = [...fruitsAndVeg]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;what will happen if we then console the above snippet 👇🏿:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruitsAndVegCopy[2].push("potatoes");
console.log(fruitsAndVegCopy) // ?
console.log(fruitsAndVeg) // ?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh! and don’t focus on the &lt;code&gt;potatoes&lt;/code&gt;  I don’t know if they are in the category of vegetable but let’s take it as it is, so the question is what do you think the output will look like? &lt;/p&gt;

&lt;p&gt;well, the output will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(fruitsAndVegCopy) // ['pinapple', 'peaches',['dodo', 'tomato', 'potatoes']]
console.log(fruitsAndVeg) // ['pinapple', 'peaches',['dodo', 'tomato', 'potatoes']]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if you guessed it correct well good for you, but do you know why? and if you guessed wrong well 😴 you will need to stay with me for a while &lt;/p&gt;

&lt;p&gt;you may be confused what in the hell is this I get it I have been there too.&lt;/p&gt;

&lt;p&gt;it all boils down on how non-primitive datatypes are stored in JavaScript, non-primitive data types in JavaScript include &lt;code&gt;arrays&lt;/code&gt;,  &lt;code&gt;objects&lt;/code&gt;,  &lt;code&gt;functions&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Heap and storage of non-primitive types in memory
&lt;/h2&gt;

&lt;p&gt;when you create a non-primitive datatype in JavaScript it is stored in a memory location called &lt;code&gt;heap&lt;/code&gt; and the variable name you’ve given that non-primitive will be used to identify the data you’ve stored in &lt;code&gt;heap&lt;/code&gt; by their memory address in &lt;code&gt;heap&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;let’s demonstrate how it works in the below snippet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arrOne = [1,2,3,4,5];
const arrTwo = [6,7,8,9,0];
const arrThree = ['a', 'b', 'c'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The diagram below shows how the arrays created will be stored in memory:&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%2Fq3p7re2bvdi5l68k8qwk.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%2Fq3p7re2bvdi5l68k8qwk.png" alt=" " width="540" height="675"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the diagram the &lt;code&gt;heap&lt;/code&gt;  is the container for our arrays actual data.&lt;/p&gt;

&lt;p&gt;The actual variable we’ve created stores reference pointing to a place in heap memory where our arrays are being stored and (this also applies to &lt;code&gt;objects&lt;/code&gt; and &lt;code&gt;function,&lt;/code&gt; ), which means the variable will store the reference of a place in memory to find the data not the actual data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shallow copy:
&lt;/h2&gt;

&lt;p&gt;We say that something is shallow copied when we have a nested non-primitive like object of arrays or arrays of object or nested arrays as the example we had before&lt;/p&gt;

&lt;p&gt;when we don’t want to mess with the original array or object and create a copy of it using the spread operator like we had in this example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruitsAndVeg = ['pinapple', 'peaches',['dodo', 'tomato' ]]
const fruitsAndVegCopy = [...fruitsAndVeg]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The values that are not arrays will be copied, because primitive stores actual values not the reference as non-primitives, &lt;/p&gt;

&lt;p&gt;and since we have a nested array inside the original array it means we have that nested arrays reference to the memory not the actual array itself as it is in the code.&lt;/p&gt;

&lt;p&gt;This is how it will be structured in the memory&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%2Fzp18sukumi09gp0qnkc1.jpg" 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%2Fzp18sukumi09gp0qnkc1.jpg" alt="Image memory structure in heap" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Shallow copy can be defined as a copy that copies the top level and on the nested level it copies its memory reference, which will point to the same memory location but with different reference&lt;/p&gt;

&lt;p&gt;the new copy will refer to the same location as the original one, this means if you change the &lt;code&gt;fruitsAndVegCopy&lt;/code&gt; nested array contents, changes will also apply to the original array because they point or refer to the same location in memory on the same data&lt;/p&gt;

&lt;p&gt;but if you change  &lt;code&gt;pineapple&lt;/code&gt; to &lt;code&gt;apple&lt;/code&gt; of  &lt;code&gt;fruitsAndVegCopy&lt;/code&gt; array they won’t have effect on the original one.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Deep copy:
&lt;/h2&gt;

&lt;p&gt;let’s also talk about Deep copy, Deep copy will copy the value from top-level to the deepest level of the object we want to clone.&lt;/p&gt;

&lt;p&gt;I will be using the &lt;code&gt;JSON&lt;/code&gt; method technique to show how to deep clone &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;JSON&lt;/code&gt; method technique:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;step 1: first &lt;code&gt;stringfy&lt;/code&gt; the object we are about to clone:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;we use the &lt;code&gt;stringfy()&lt;/code&gt; method of &lt;code&gt;JSON&lt;/code&gt; to make the object a string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ['apple', 'mango', 'banana'];
const fruitsAndVeg = ['pinapple', 'peaches',['dodo', 'tomato' ]];
const stringfiedarr = JSON.stringfy(fruitsAndVeg); // '["pinapple","peaches",["dodo","tomato"]]'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;our new stringfied array version would look like this:&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%2Ftj8dwllthupdxiliohkf.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%2Ftj8dwllthupdxiliohkf.png" alt=" " width="609" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;step 2: Convert the object from a string version to a non-primitive type again:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;the &lt;code&gt;parse()&lt;/code&gt;  method of &lt;code&gt;JSON&lt;/code&gt;  is used to convert back converted object back to normal objects or non-primitive type&lt;/p&gt;

&lt;p&gt;and then store that array into a new variable which will create a new memory reference for our array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = ['apple', 'mango', 'banana'];
const fruitsAndVeg = ['pinapple', 'peaches',['dodo', 'tomato' ]]; 
const fruitAndVegCopy = JSON.parse(stringfiedObject)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;after the conversion is done we will create a new memory location assign its address to &lt;code&gt;fruitAndVegCopy&lt;/code&gt; which will be completely different from the original array allowing us to manipulate it without having to worry about the original array mutability&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%2Frzidkzor0x1d69mnj9ci.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%2Frzidkzor0x1d69mnj9ci.png" alt="Image deepCopy illustration" width="348" height="61"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts:
&lt;/h2&gt;

&lt;p&gt;if you find this way of deep clone or copy using &lt;code&gt;JSON&lt;/code&gt; you can read more about this you can read more about the method to deep clone called &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone" rel="noopener noreferrer"&gt;structuredClone&lt;/a&gt; method which does the same as the &lt;code&gt;JSON&lt;/code&gt; does&lt;/p&gt;

&lt;p&gt;conclusion:&lt;/p&gt;

&lt;p&gt;I hope you got something out of this and have grasped the concept of deep and shallow copy in JavaScript, I would appreciate sharing with me what you gained throughout this whole article and what do you think about the content in general.&lt;/p&gt;

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