<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: OzIheb</title>
    <description>The latest articles on DEV Community by OzIheb (@oziheb).</description>
    <link>https://dev.to/oziheb</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%2F893341%2Fc42da398-20c2-479d-896d-88aa2b688456.png</url>
      <title>DEV Community: OzIheb</title>
      <link>https://dev.to/oziheb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oziheb"/>
    <language>en</language>
    <item>
      <title>Closure in JavaScript Explanation done RIGHT</title>
      <dc:creator>OzIheb</dc:creator>
      <pubDate>Sat, 23 Jul 2022 21:32:00 +0000</pubDate>
      <link>https://dev.to/oziheb/closure-in-javascript-explanation-done-right-4g7j</link>
      <guid>https://dev.to/oziheb/closure-in-javascript-explanation-done-right-4g7j</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Closure is arguably the most essential part of javascript to understand, once you do get it everything else will naturally fall into place.&lt;br&gt;
Closure is often used in :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Data-privacy with objects pro-level functions like &lt;strong&gt;once&lt;/strong&gt; that allows you to run a function one time only.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;event handlers in asynchronous javascript which I will get into in a different article.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use closure to hide away functionality that you generally do not want people to mess with.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;Simply put, when you return a function ( from another function call ) in JavaScript you will also return with it a &lt;em&gt;"Backpack"&lt;/em&gt; of saved data ( Part of the local memory of the parent function execution context that the returned function uses ).&lt;/p&gt;

&lt;p&gt;Hold on just a minute, returning a function? Execution context? A function with a backpack??&lt;br&gt;
Do not worry we will answer all these questions and more below.&lt;/p&gt;
&lt;h2&gt;
  
  
  Execution context
&lt;/h2&gt;

&lt;p&gt;When a function is called the JavaScript interpreter starts what you can call a "sub-program" that has it own state ( memory ) where it stores it locally defined variables and passed arguments and it own thread of execution ( the method javascript uses to interpret code, going through it line by line ) where you find the function specific instructions ( code ).&lt;br&gt;
For example, take a look at the following code block&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myLibrary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isLibraryEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;library&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;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;library&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;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;library is empty&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;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;library is not empty&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="p"&gt;};&lt;/span&gt;
&lt;span class="nf"&gt;isLibraryEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myLibrary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On line 6 the function is called which triggers the creation of a brand new execution context for it as the code is being run which will have it own local state ( as in local memory )&lt;br&gt;
that will store the following :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;library : myLibrary&lt;/li&gt;
&lt;li&gt;length : 0 ( since the array is empty )&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjoxdog7dlvfk410l0jeg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjoxdog7dlvfk410l0jeg.png" alt="showing isLibraryEmpty execution context"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;then it will execute the condition check which will lead to the console log of "library is not empty" followed by the deletion of the execution context including all of it &lt;strong&gt;local state.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Returning a function from another function
&lt;/h2&gt;

&lt;p&gt;For more clarity, review this modified version of our earlier example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myLibrary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isLibraryEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;library&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;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;library&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;checker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;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;library is empty&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;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;library is not empty&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="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;newfunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;isLibraryEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myLibrary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;newfunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="c1"&gt;//"library is empty"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;const newfunc = isLibraryEmpty(myLibrary)&lt;/code&gt; the isLibraryEmpty execution context will run eventually returning to newfunc a function that checks if the myLibrary array &lt;strong&gt;specifically&lt;/strong&gt; is empty or not then all the local memory stored in the execution context will be deleted&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F11eovz4ifefvz0znx8tj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F11eovz4ifefvz0znx8tj.png" alt="Showing newfunc execution context"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you run the following code but at add at line 6 &lt;code&gt;myLibrary.push("test")&lt;/code&gt; you will notice that it console logs that the library is not empty anymore.&lt;br&gt;
But hold on! How could it do that when we have established clearly that the isLibraryEmpty execution context was deleted which means all of the local memory was also gone? How could the length have incremented to 1 knowing that?&lt;/p&gt;

&lt;h2&gt;
  
  
  What it means to have closure?
&lt;/h2&gt;

&lt;p&gt;Continuing from the previous section, the truth is, when we returned the function we returned with it what you can call a backpack that contains some important &lt;strong&gt;data&lt;/strong&gt; that the returned function requires to run, in another words that data is &lt;strong&gt;persistant&lt;/strong&gt;, it is not the data that was deleted in the isLibraryEmpty execution context, it is a copy or a &lt;strong&gt;reference&lt;/strong&gt; of that data.&lt;br&gt;
Only newfunc function has access to that backpack, the data in it is &lt;strong&gt;lexically scoped&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In technical words, the function when it returned it brought with it a &lt;strong&gt;P.L.S.R.D&lt;/strong&gt; (&lt;strong&gt;P&lt;/strong&gt;ersistant &lt;strong&gt;L&lt;/strong&gt;exically &lt;strong&gt;S&lt;/strong&gt;coped &lt;strong&gt;R&lt;/strong&gt;eferenced &lt;strong&gt;D&lt;/strong&gt;ata) also known as &lt;strong&gt;closure&lt;/strong&gt; as you can see in the figure below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cemow0eysfka4b3rttv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cemow0eysfka4b3rttv.png" alt="Showing closure at work"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Review
&lt;/h2&gt;

&lt;p&gt;To sum it up, closure, is a javascript feature that allows us to return lexically scoped data from the invoked function local memory with the returned function X so X can run without any errors or issues.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>3 Javascript utility functions to show you why they matter</title>
      <dc:creator>OzIheb</dc:creator>
      <pubDate>Sun, 17 Jul 2022 13:29:01 +0000</pubDate>
      <link>https://dev.to/oziheb/3-javascript-utility-functions-to-show-you-the-what-why-and-when-they-are-used-59ob</link>
      <guid>https://dev.to/oziheb/3-javascript-utility-functions-to-show-you-the-what-why-and-when-they-are-used-59ob</guid>
      <description>&lt;p&gt;Many developers, especially juniors and beginners push understanding js utility functions like map(), reduce() to the side not realising that once you &lt;em&gt;get it&lt;/em&gt; you will start seeing ways to use them to improve your code &lt;strong&gt;everywhere&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are JS utility functions?
&lt;/h2&gt;

&lt;p&gt;A utility function is : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A standalone function ( it is not a member of any class and is in the global namespace )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Has no side effects ( It does not change any of it parameters or any global variable )&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It output is directly dependent on it input.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why and When Utility Functions?
&lt;/h2&gt;

&lt;p&gt;As mentioned earlier, utility functions are &lt;strong&gt;highly reuseable&lt;/strong&gt; to perform common tasks.&lt;/p&gt;

&lt;p&gt;They simplify things by making it easier to refactor code and minimize it.&lt;/p&gt;

&lt;p&gt;Utility functions are generic snippets that will make your code more efficient.&lt;/p&gt;

&lt;p&gt;Utilizing utility function is a great way to remain consistent across your projects and when you want to modify the underlying functionality, you only need to modify it from one place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do something an X number of times
&lt;/h2&gt;

&lt;p&gt;A prevalent task to do in javascript is to repeat something many times.&lt;br&gt;
Using the &lt;code&gt;times(_numberOfTimes_ , _function_)&lt;/code&gt; will do exactly that in a clean, practical way.&lt;/p&gt;

&lt;p&gt;You can implement this functionality by using _. helper library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Remove duplicates from an array
&lt;/h2&gt;

&lt;p&gt;There are many utility functions for modifying arrays as you would expect which you can find on &lt;a href="https://underscorejs.org/#arrays"&gt;here&lt;/a&gt;&lt;br&gt;
One useful one is &lt;code&gt;removeDuplicates(array)&lt;/code&gt; it returns a new array with all the duplicates stripped away.&lt;/p&gt;

&lt;h2&gt;
  
  
  Find the difference of two arrays
&lt;/h2&gt;

&lt;p&gt;Finds the repeating values in both arrays and return them in a string just by typing a simple one liner &lt;code&gt;intersection(arr1,arr2, ...args)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Utility functions are a sea of information and it is very easy to get lost in it, so becareful and be wary of the bloat that you may unintentionally add with implementing all these libraries like lodash.&lt;/p&gt;

&lt;p&gt;Personally, I recommend that you build your own lightweight utility functions library, adding only the ones you find extremely useful.&lt;/p&gt;

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