<?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: MirAli Mobasheri</title>
    <description>The latest articles on DEV Community by MirAli Mobasheri (@alimobasheri).</description>
    <link>https://dev.to/alimobasheri</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%2F489317%2F66e48cc5-284d-43ca-89b9-6bd442830693.jpg</url>
      <title>DEV Community: MirAli Mobasheri</title>
      <link>https://dev.to/alimobasheri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alimobasheri"/>
    <language>en</language>
    <item>
      <title>Tailwind in Reality: Bringing CSS Classes to Life with AI Videos!</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Tue, 22 Jul 2025 15:47:39 +0000</pubDate>
      <link>https://dev.to/alimobasheri/tailwind-in-reality-bringing-css-classes-to-life-with-ai-videos-40li</link>
      <guid>https://dev.to/alimobasheri/tailwind-in-reality-bringing-css-classes-to-life-with-ai-videos-40li</guid>
      <description>&lt;p&gt;Hey folks! 👋&lt;/p&gt;

&lt;p&gt;I just dropped a mini-series on YouTube called &lt;strong&gt;“Tailwind in Reality”&lt;/strong&gt;, where I used ChatGPT's &lt;strong&gt;Sora AI&lt;/strong&gt; to turn four Tailwind CSS classes into real-world magic.&lt;/p&gt;

&lt;p&gt;For example: &lt;strong&gt;rounded-full&lt;/strong&gt; → a square pizza becomes a perfect circle&lt;/p&gt;

&lt;p&gt;Imagine a world where every action, every shape, and every motion is driven by a Tailwind CSS class—buttons float, objects morph, and interfaces blur into reality. 🌀&lt;/p&gt;

&lt;p&gt;Check it out below and let me know which transformation blew your mind! 🚀&lt;/p&gt;

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

&lt;h2&gt;
  
  
  How It Was Made
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Videos:&lt;/strong&gt; Sora AI (ChatGPT Plus, 20$ subscription)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Voiceovers:&lt;/strong&gt; ChatGPT Advanced Voice Mode
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Music:&lt;/strong&gt; SunoAI
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SFX:&lt;/strong&gt; Transitions &amp;amp; ambient from freesound.org
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ve just launched my channel, &lt;strong&gt;MirAli Makes&lt;/strong&gt;, to explore more AI experiments, dev tutorials, and creative projects. I’d love your feedback—what Tailwind class should come to life next? Drop your ideas below! 🎨✨&lt;/p&gt;

</description>
      <category>tailwindcss</category>
      <category>webdev</category>
      <category>ai</category>
      <category>frontend</category>
    </item>
    <item>
      <title>JavaScript: The Klassic Way!</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Sun, 21 Jan 2024 06:57:33 +0000</pubDate>
      <link>https://dev.to/alimobasheri/javascript-the-klassic-way-pfg</link>
      <guid>https://dev.to/alimobasheri/javascript-the-klassic-way-pfg</guid>
      <description>&lt;p&gt;Before there was the class keyword, there was the constructor function. And now that there are classes, the constructors are still there. Indeed the &lt;code&gt;classes&lt;/code&gt; are only a syntactic sugar for what constructor functions have been doing for a long time. &lt;/p&gt;

&lt;p&gt;This post is about the Klassic JS, an era before the &lt;code&gt;class&lt;/code&gt; keyword existed. When developers used &lt;code&gt;constructor&lt;/code&gt; functions  to implement &lt;code&gt;Object-Oriented Behavior&lt;/code&gt;. But this doesn't mean it's useless. All the things we learn about, in this article, are actually how JS still works under the hood.&lt;/p&gt;

&lt;p&gt;We'll start by reviewing how special data types likes &lt;em&gt;Functions&lt;/em&gt; and &lt;em&gt;Objects&lt;/em&gt; are treated in JS. Then we'll progress through creating objects using &lt;em&gt;Constructor Functions&lt;/em&gt;. And in the end, we'll learn how JS implements some &lt;em&gt;advanced OOP patterns&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Functions And Objects
&lt;/h2&gt;

&lt;p&gt;When learning about  JS' OOP, objects are the things we'll be talking about. But functions are the ones we'll be using to build those things.&lt;/p&gt;

&lt;p&gt;Let's have a quick look at how these two relate in JS.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.1 Everyone Is A First-Class Citizen
&lt;/h3&gt;

&lt;p&gt;Suppose you've a string variable named &lt;code&gt;a&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can check how many characters it has using its &lt;code&gt;length&lt;/code&gt; property.&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="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;a&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="c1"&gt;// 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's suppose you've an array, named &lt;code&gt;b&lt;/code&gt;. You can still check how many elements it has using the same property, &lt;code&gt;length&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&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="s1"&gt;Hello World!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&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="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="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if we define a function and try to log its &lt;code&gt;length&lt;/code&gt;? Does it still work? Let's test it with an add function.&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;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;add&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="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The log command in the above code block, will output 2. It's how many arguments the function takes (&lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;). This means that the function we defined has its own set of properties and methods. Like any other of the value types we checked.&lt;/p&gt;

&lt;p&gt;This is a simple explanation of what &lt;code&gt;Functions As First-Class Citizens&lt;/code&gt; in JS means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Functions in JS aren't treated like some subroutine to be called later. They're stored as an &lt;code&gt;object&lt;/code&gt;. We can store them into variables, pass as arguments and return from function calls.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But, what we're most interested in, is how we can use them as &lt;code&gt;Object Constructors&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;More on that later.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.2 So Everything Is An Object?
&lt;/h3&gt;

&lt;p&gt;Well, not everything. As a matter of the fact, the string variable, we defined (&lt;code&gt;a&lt;/code&gt;), is known as a primitive value. So, it's an exact value stored somewhere in memory and assigned to a variable's memory address.&lt;/p&gt;

&lt;p&gt;But how does &lt;code&gt;a.length&lt;/code&gt; work, if it's a simple value rather than an object?&lt;/p&gt;

&lt;p&gt;Any time you try to access a property or a method on a primitive value, JS will provide an &lt;code&gt;Object Wrapper&lt;/code&gt; for the value. It uses that wrapper to give you access to the methods and properties.&lt;/p&gt;

&lt;p&gt;So, while everything is not an Object, many things are treated as one. This means objects are a core part of JS. That makes them important when interacting with the language's API.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Constructing Objects
&lt;/h2&gt;

&lt;p&gt;In most Object-Oriented programming languages, &lt;code&gt;blueprint&lt;/code&gt; is a keyword in explaining OOP structures. But this isn't the case for JS.&lt;/p&gt;

&lt;p&gt;In JavaScript, the keyword is &lt;code&gt;prototyping&lt;/code&gt;. This is how the language, shapes the objects's structure and behavior. There's a prototypal inheritance chain in the background. It specifies what properties and methods are accessible by an &lt;code&gt;Object Instance&lt;/code&gt;. This simplifies the process of sharing properties among different instances of an object.&lt;/p&gt;

&lt;p&gt;We create these instances using constructor functions. So, before we get deep with the prototypes, let's see what a constructor is and how it works.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.1. Functions Will Do The Job
&lt;/h3&gt;

&lt;p&gt;Constructors are functions. You can define a function. Call it with a &lt;code&gt;new&lt;/code&gt; keyword, and congratulations! You've used a constructor. &lt;/p&gt;

&lt;p&gt;But in order for a constructor to be a useful one, you should write a useful function. How's that? Let's look at some examples.&lt;/p&gt;

&lt;p&gt;Do you remember the &lt;code&gt;add&lt;/code&gt; function? Let's recall the 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;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function simply takes two numbers and returns a new one. We can call it with a &lt;code&gt;new&lt;/code&gt; keyword, if we want to.&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;adder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this line has no meaning. The adder is an empty Object. Of course not an entirely empty object. We'll get to that later. But in our case, this line is meaningless. In order to write a useful constructor function, we need to use the &lt;code&gt;this&lt;/code&gt; keyword. &lt;/p&gt;

&lt;p&gt;By using &lt;code&gt;this&lt;/code&gt; we can access the instance of the new object that is created when the function is called with the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;But first let's check what an instance is.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.2 I have an instance, therefore I exist!
&lt;/h3&gt;

&lt;p&gt;We're all Humans. &lt;/p&gt;

&lt;p&gt;Indeed, &lt;code&gt;human&lt;/code&gt; is a species name. We all share some traits which differentiate us from other species and animals. We can walk on two legs, we have two eyes, and we can speak in different languages. &lt;/p&gt;

&lt;p&gt;While all of us are humans, we're not the same human. Each of us are unique. Our birthday is different. Our parents are different. We're born in different locations, and we even have different names. &lt;/p&gt;

&lt;p&gt;The difference is what we can call the &lt;code&gt;identity&lt;/code&gt;. You might be of the same type as the others, but you've got a different identity.&lt;/p&gt;

&lt;p&gt;The constructor functions, will produce new Objects. These objects will share some traits, but they're not the same production. Each of them is located in a different memory address from the others. &lt;/p&gt;

&lt;p&gt;So each instance of an object, is a working example of that object that is stored in a specific memory address.&lt;/p&gt;

&lt;p&gt;Now we can finally try creating an object with a constructor function. And see where it goes.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.3 &lt;code&gt;new&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;We already learned the &lt;code&gt;add&lt;/code&gt; function was not usable. That's because calling it with the &lt;code&gt;new&lt;/code&gt; keyword returned an empty object. But where does this object come from, and why the hell does the &lt;code&gt;new&lt;/code&gt; keyword even work?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword always creates an empty new Object. Why? So, it can give us access to that object, inside the function call. &lt;/p&gt;

&lt;p&gt;Let's illustrate it through steps:&lt;/p&gt;

&lt;p&gt;1 You call the function with the new keyword:&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;adder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2 Now when this code is getting interpreted, as the JS engine reaches the &lt;code&gt;new&lt;/code&gt; keyword, it creates an empty object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*** INTERPRETER MEMORY

ADDRESS: 0x123456789 
=&amp;gt; STORE NEW OBJECT REFERENCE 
=&amp;gt; PROPERTIES: []

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

&lt;/div&gt;



&lt;p&gt;This is a very simple illustration of the memory allocation of the new object that is created. This object has no properties, which is shown as an empty array.&lt;/p&gt;

&lt;p&gt;3 The function is called, and the new object is passed into it as the &lt;code&gt;this&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
*** INTERPRETER MEMORY

ADDRESS: 0x22222222 
=&amp;gt; FUNCTION DEFINITION 
=&amp;gt; CALL FUNCTION BODY 
=&amp;gt; THIS = 0x123456789

... run the rest of function body
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, when trying to access the &lt;code&gt;this&lt;/code&gt; keyword inside the function block, it will give you an empty object.&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="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// {}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;this&lt;/code&gt; is the object that &lt;code&gt;new&lt;/code&gt; created.&lt;/p&gt;

&lt;p&gt;4 The interpreter runs your function. It evaluates everything and then assigns the value for the &lt;code&gt;adder&lt;/code&gt; variable. But what value? Will it be the sum of the two numbers? No. It will be the object which was passed to this. In case of our function, an empty object.&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="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;adder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// {}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thus, an object is created on the air, and after the function is called, the same object is assigned to the variable. &lt;/p&gt;

&lt;p&gt;But what good is this new object for us? The answer is, we can mutate this object. We can add properties to it, and shape what we need to be returned. &lt;/p&gt;

&lt;p&gt;Finally, we're on track!&lt;/p&gt;

&lt;h3&gt;
  
  
  2.4 &lt;code&gt;this&lt;/code&gt; Is An Object
&lt;/h3&gt;

&lt;p&gt;Objects in JS are mutable. This means, they don't have private or strict members. You can always change a member of an object. Or assign members and values to it.&lt;/p&gt;

&lt;p&gt;To create a new object in our constructor function, we've to utilize this behavior. We're given an empty object via the &lt;code&gt;this&lt;/code&gt; keyword and then we can shape it as our needs.&lt;/p&gt;

&lt;p&gt;So, let's change our add function to make it a useful &lt;code&gt;constructor&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;second_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we don't need extra steps to get access to the object stored in &lt;code&gt;this&lt;/code&gt;. We can start on the fly and try adding members to it.&lt;/p&gt;

&lt;p&gt;We've added three members to the Object. The &lt;code&gt;first_number&lt;/code&gt; and &lt;code&gt;second_number&lt;/code&gt; properties store the passed arguments in the object. The &lt;code&gt;sum&lt;/code&gt; property is the result of adding them.&lt;/p&gt;

&lt;p&gt;Next, we call the function with the new keyword:&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;adder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sum&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;Voila! We've done the equivalent of creating a class using the &lt;code&gt;class&lt;/code&gt; keyword in ES6+. Indeed we've done what the &lt;code&gt;class&lt;/code&gt; keyword actually does under the hood.&lt;/p&gt;

&lt;p&gt;Let's try creating another object using the &lt;code&gt;add&lt;/code&gt; constructor.&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;adder2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="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;adder2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sum&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;So, we did it. Now what?&lt;/p&gt;

&lt;h2&gt;
  
  
  3. JS OOP: Behind The Scenes
&lt;/h2&gt;

&lt;p&gt;By this point, we have learned about many things. Including &lt;code&gt;constructors&lt;/code&gt;, &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;this&lt;/code&gt; keywords, and how functions are objects.&lt;/p&gt;

&lt;p&gt;In this section we'll dive deeper into the topics concerning how JavaScript thinks about these processes. In short, we want to grasp the concepts!&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1 Prototypes (again)
&lt;/h3&gt;

&lt;p&gt;I mentioned the &lt;code&gt;prototype&lt;/code&gt; earlier in this post. It defines how OOP behaviors like &lt;code&gt;inheritance&lt;/code&gt; and &lt;code&gt;polymorphism&lt;/code&gt; are implemented in JS.&lt;/p&gt;

&lt;p&gt;We'll first have a look at how prototypes can be inspected and altered.&lt;/p&gt;

&lt;h4&gt;
  
  
  3.1.1 Defining Methods
&lt;/h4&gt;

&lt;p&gt;All that our &lt;code&gt;adder&lt;/code&gt; objects have are two number properties with their sum as a third one. This is cool, but not a very useful thing. In a dynamic programming environment, functions are more useful.&lt;/p&gt;

&lt;p&gt;What if &lt;code&gt;adder.first_number&lt;/code&gt; is updated by mutation, but &lt;code&gt;adder.sum&lt;/code&gt; stays the same value? We can change &lt;code&gt;sum&lt;/code&gt; into a function:&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;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;second_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;second_number&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;We can create a new instance and check its properties:&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;adder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output the following object:&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="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;first_number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;second_number&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sum&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;f&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;So, we can call &lt;code&gt;adder.sum&lt;/code&gt; whenever we want and have access to the latest sum of the two numbers in the adder:&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="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;adder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="nx"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&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="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;adder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And as you can see, the &lt;code&gt;sum&lt;/code&gt; function has access to the latest value of &lt;code&gt;first_number&lt;/code&gt;. So, it will return correct values after &lt;code&gt;first_number&lt;/code&gt; is updated.&lt;/p&gt;

&lt;h4&gt;
  
  
  3.1.2 In Search Of A Better Solution
&lt;/h4&gt;

&lt;p&gt;Do you remember when we talked about instances? In our previous examples each of &lt;code&gt;adder1&lt;/code&gt; and &lt;code&gt;adder2&lt;/code&gt; are instances of the &lt;code&gt;add&lt;/code&gt; function. Instance means they each have a new memory address assigned to them.&lt;/p&gt;

&lt;p&gt;This also means every property and method that is a member of  this object is stored in a new memory address specific to it. This includes the method definitions too. While this can seem reasonable, it isn't the most efficient approach.&lt;/p&gt;

&lt;p&gt;Efficiency in this case is concerned with the fact that each time we create a new instance of add, we also utilize a new memory address for the sum method. This is because, as we learned through this post, function definitions are objects and first class citizens. So they will require to be redefined every time.&lt;/p&gt;

&lt;p&gt;But what if there was a way we could define this method only once and access it in every instance? Because a function like &lt;code&gt;sum&lt;/code&gt; doesn't change every time a new instance of add is created. &lt;/p&gt;

&lt;p&gt;The definition is always the same. The only thing that changes is the value of &lt;code&gt;this&lt;/code&gt;. The solution in this case is the object &lt;code&gt;prototype&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's travel in time and return our constructor function to the state when it didn't still have the sum function definition. We also remove the sum property.&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;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;second_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you think will happen if we call &lt;code&gt;adder.sum()&lt;/code&gt;? It surely throws an error indicating that sum is &lt;code&gt;undefined&lt;/code&gt;. But what will exactly happen, before JS throws the error? Does it only check the adder's instance to find sum? The answer is no. It will also look into &lt;code&gt;adder.prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the next section. We'll review the exact order of what happens under the hood.&lt;/p&gt;

&lt;h4&gt;
  
  
  3.1.3 The Prototypal Chain
&lt;/h4&gt;

&lt;p&gt;When we call &lt;code&gt;adder.sum()&lt;/code&gt; on a version of it that doesn't have &lt;code&gt;sum&lt;/code&gt; function defined, JS will do a chain of searches before it returns to us and identifies &lt;code&gt;sum&lt;/code&gt; as &lt;code&gt;undefined&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;First of all, keep this in mind:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every object in JS has a link to another object, which is known as its prototype.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then let's visualize how the JS engine looks for the sum method. For this, we can log the objects in our devtools console to check the output:&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;adder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will output the following object:&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;first_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;second_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above output displays the &lt;code&gt;own properties&lt;/code&gt; of the object. Own properties only exist on this instance of the object and have the highest priority in the prototypal chain. The JS engine will look into these properties and doesn't find &lt;code&gt;sum&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But it doesn't lose hope, and looks further, into another object. But which one?&lt;/p&gt;

&lt;p&gt;The above output is what the console.log typically shows of an object which includes only the &lt;code&gt;own properties&lt;/code&gt;. Our object has another property which isn't listed here. It's the prototype which we can access and read in three ways: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;By opening the browser devtools console and typing the above console.log command.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using &lt;code&gt;Object.getPrototypeOf(adder)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;With &lt;code&gt;console.log(adder.[[Prototype]])&lt;/code&gt;. This is the property key which stores the prototype object in an instance. Every object has this key.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We'll follow with the second approach as it's a clean one. We run the following statement:&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="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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which results in:&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;f&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;Where does this prototype come from? As every function in JS is an object, every one of these functions has a prototype which initially has one important method. It's the &lt;code&gt;constructor&lt;/code&gt; we see in the above output. It's the &lt;code&gt;add&lt;/code&gt; function we've defined. &lt;/p&gt;

&lt;p&gt;So, every instance of objects returned by a constructor can actually access its constructor function using &lt;code&gt;instance.[[Prototype]].constructor&lt;/code&gt;. But it can also access any other property or method set in prototype.&lt;/p&gt;

&lt;p&gt;The JS engine will look into this prototype object and still doesn't find a property named &lt;code&gt;sum&lt;/code&gt;. So, what next? Does it print the error? No. The engine will continue searching. This time in &lt;code&gt;adder.[[Prototype]].[[Prototype]]&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But what is this second protoytpe?&lt;/p&gt;

&lt;p&gt;Every constructor prototype, when created, has a link to the global &lt;code&gt;Object&lt;/code&gt; namespace of JS. Object is an object by itself. But it's used to create other objects and is the base prototype of all objects in JS.&lt;/p&gt;

&lt;p&gt;Let's inspect a log of it:&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;adderPrototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adder&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adderPrototype&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you notice &lt;code&gt;Object.getPrototypeOf&lt;/code&gt;? Yes it's the same Object, whose prototype is available in every JS object. The output of the above command looks like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  constructor,
  hasOwnProperty,
  isPrototypeOf,
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a lot of utility methods in this prototype, which we haven't included here. But they're not our concern in this article. What we want to know is what will happen if JS doesn't find the sum method here too?&lt;/p&gt;

&lt;p&gt;Well, it will look into prototype of this object. You might think we'll keep this loop forever. But it will end right here. That's because &lt;code&gt;Object.[[Prototype]].[[Prototype]]&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt;. Which means it isn't linked to any other object.&lt;/p&gt;

&lt;p&gt;So every object is linked to another object, if its prototype is an object. If the prototype is set to null, it's safe to say that the object isn't linked to another one.&lt;/p&gt;

&lt;p&gt;This is the end of the chain. JS will throw an error and the program exits.&lt;/p&gt;

&lt;p&gt;But we've still got things to do!&lt;/p&gt;

&lt;h4&gt;
  
  
  3.1.4 Adding Members To The Prototype
&lt;/h4&gt;

&lt;p&gt;Like how we altered the &lt;code&gt;this&lt;/code&gt; keyword inside the functions we can alter the prototypes too. To do so how we have different ways. For example as we have been using the &lt;code&gt;Object.getPrototypeOf&lt;/code&gt; for retrieving the prototype object we can use &lt;code&gt;Object.setPrototypeOf&lt;/code&gt; for updating it. &lt;/p&gt;

&lt;p&gt;You might also be tempted to mutate the &lt;code&gt;[[Prototype]]&lt;/code&gt; object directly. You can do that too. You can set it to a completely different object, create your own chain of inheritances or even make it null, so the prototype won't affect your object.&lt;/p&gt;

&lt;p&gt;This means it's a dirty but yet powerful concept. In this section we only want to learn how methods are added to the prototype so they're defined once and accessible in every instance of the object.&lt;/p&gt;

&lt;p&gt;Indeed we saw the solution to this problem in the last section, when we were journeying through the prototype chain. You might have noticed when we checked prototype of the &lt;code&gt;adder&lt;/code&gt; we got an object that had an instance to the &lt;code&gt;constructor&lt;/code&gt; function. This is the best place for storing methods or static properties. Since it's only related to the constructor function and is available to every instance in the first level of prototypal chain.&lt;/p&gt;

&lt;p&gt;We can mutate the prototype and add the sum method. It's done quit easily by &lt;code&gt;add.prototype.sum = ...&lt;/code&gt;. This is a convenient way to add methods to constructor functions in JS.&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;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;second_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;second_number&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 easily check that sum works:&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;adder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;

&lt;span class="nx"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;adder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 102&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was all that we needed to do. But there's one last thing to check out. The fact that sum works correctly in this example means that when JS finds a method in the prototypal chain of an inheriting object, the &lt;code&gt;this&lt;/code&gt; keyword inside that method always points to the inheriting object. &lt;/p&gt;

&lt;p&gt;In our example, the &lt;code&gt;inheriting&lt;/code&gt; object is &lt;code&gt;adder&lt;/code&gt; and the method is in the prototype of the function &lt;code&gt;add&lt;/code&gt;'s object. But &lt;code&gt;this&lt;/code&gt; doesn't point to the function's object. It points to the object, the search started from.&lt;/p&gt;

&lt;p&gt;Over.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2 Inheritance and Polymorphism
&lt;/h3&gt;

&lt;p&gt;So far, we can create objects from functions, and we can assign methods to them. We have even learned about the Prototypal chain. But did we go through all that hassle, only to learn how to define shared methods for our objects?&lt;/p&gt;

&lt;p&gt;No. We can do more. Indeed prototypes make us able to customize our objects in more complex ways. These include inheriting and polymorphism. These are both advanced OOP concepts well-known in most of OOP languages.&lt;/p&gt;

&lt;p&gt;First of all we want to learn what inheritance is.&lt;/p&gt;

&lt;h4&gt;
  
  
  3.2.1 The Problem: Create More Objects, Write Less Code.
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;add&lt;/code&gt; function does its job very great. And all our &lt;code&gt;adders&lt;/code&gt; work perfectly. But what if we wanted to create a type of adder objects which also keep a record of every time a sum was called and the values had been changed.&lt;/p&gt;

&lt;p&gt;We can do it in many ways. We can create an absolutely new constructor function, which does the job for us. Or we can change the add function so it keeps the records. But what if we don't want the adders to be able to do more than what they are currently capable of? &lt;/p&gt;

&lt;p&gt;Yet the add function shares some of the code we need for the new objects. So, the third way will be a combination of these two. &lt;/p&gt;

&lt;p&gt;We don't rewrite the part of code that is shared among the objects. But we create new objects, which in addition to the initial properties can do more advanced things.&lt;/p&gt;

&lt;p&gt;Prototypes come to our help. Again.&lt;/p&gt;

&lt;h4&gt;
  
  
  3.2.2 A New Function.
&lt;/h4&gt;

&lt;p&gt;We start by creating a new constructor. We name it &lt;code&gt;addRecord&lt;/code&gt;. As it's the same as add with the difference it records the changes.&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;addRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our function's body, is empty, because we don't yet know how to handle it. First of all we know that we need to reuse the &lt;code&gt;add&lt;/code&gt; function. Maybe we can try calling the add function inside &lt;code&gt;addRecord&lt;/code&gt;? Let's try that:&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;addRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;adder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// But where do we put adder?&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we can try creating a new instance of add using new. We even stored it in a variable. But there's a problem. The variable is not a part of our object's instance. Maybe we can try assigning the properties of adder to our &lt;code&gt;this&lt;/code&gt; object? But that's not clean and what if we had a hundred properties? Spreading? Seriously? No. These solutions only add references to the instance created by adder. This is not what we want. &lt;/p&gt;

&lt;p&gt;Whatever approach we use, we're not going to get a clean object. While addRecord reuses add, it shouldn't treat add as a different object. Indeed it's an altered version of add. It should inherit the behaviors of add.&lt;/p&gt;

&lt;p&gt;But how? JS has two interesting methods available for us. They're the &lt;code&gt;call&lt;/code&gt; and the &lt;code&gt;apply&lt;/code&gt;. Our saviors.&lt;/p&gt;

&lt;h4&gt;
  
  
  3.2.2 &lt;code&gt;this&lt;/code&gt;. Again.
&lt;/h4&gt;

&lt;p&gt;Some years ago, when I was reading about the &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;apply&lt;/code&gt; methods for the first time I had a serious question. While the definitions were clear and I exactly knew what they did, I still couldn't understand what their use case can be.&lt;/p&gt;

&lt;p&gt;Well, it turned out they were mainly used in OOP patterns. And they do their job beautifully.&lt;/p&gt;

&lt;p&gt;Here's how MDN defines the &lt;code&gt;call&lt;/code&gt; method:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The call() method of Function instances calls this function with a given &lt;code&gt;this&lt;/code&gt; value and arguments provided individually.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The syntax looks like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;thisArg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;,...&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;argN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What &lt;code&gt;apply&lt;/code&gt; does is identical to what call does. It actually doesn't do anything different. The only difference is in the syntax. Apply accepts the arguments as an array. it doesn't take them separately. here's the syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;thisArg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;,...&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;argN&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;thisArg&lt;/code&gt; is what we're most interested in. It lets us do what we did with new with one difference. &lt;code&gt;new&lt;/code&gt; passed a new object as &lt;code&gt;this&lt;/code&gt; to &lt;code&gt;add&lt;/code&gt;. But with &lt;code&gt;call&lt;/code&gt; we can pass any object we want as &lt;code&gt;this&lt;/code&gt;. I think you can guess it now.&lt;/p&gt;

&lt;p&gt;We already have a &lt;code&gt;this&lt;/code&gt; when &lt;code&gt;addRecord&lt;/code&gt; is called with new. Why not pass it to &lt;code&gt;add&lt;/code&gt; and let it add &lt;code&gt;first_number&lt;/code&gt; and &lt;code&gt;second_number&lt;/code&gt; properties to the instance created by &lt;code&gt;addRecord&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Okay, we'll do it!&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;addRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then let's create an instance of addRecord and log its properties:&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;addRecorder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;addRecord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addRecorder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will output the following object:&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;first_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;second_number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if we inspect its prototype we'll see, it only contains the constructor of &lt;code&gt;addRecord&lt;/code&gt;. There's no sign of the &lt;code&gt;sum&lt;/code&gt; function again. But don't worry! We don't need to look for it through the whole chain. We'll add it to the chain. &lt;/p&gt;

&lt;p&gt;How? We manually link the prototype of &lt;code&gt;addRecord&lt;/code&gt; to &lt;code&gt;add&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// after we've defined addRecord&lt;/span&gt;
&lt;span class="nx"&gt;addRecord&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, when we later call &lt;code&gt;addRecorder.sum()&lt;/code&gt;, it will be found down a deep chain of prototypes. (You can figure it out by yourself)&lt;/p&gt;

&lt;p&gt;Mission accomplished.&lt;/p&gt;

&lt;h4&gt;
  
  
  3.2.3 Everything is a Prototype of a Prototype of a Prototype!
&lt;/h4&gt;

&lt;p&gt;Directly setting a prototype's prototype to a new object, looks a very interesting example of how inheritances can be implemented and mutated in JS.&lt;/p&gt;

&lt;p&gt;By now we've only written one line inside &lt;code&gt;addRecord&lt;/code&gt; and one line outside of it. And yet we've totally inherited every property and method of &lt;code&gt;add&lt;/code&gt;. The next step is to define the recording functionality.&lt;/p&gt;

&lt;p&gt;For this purpose we add a new property to &lt;code&gt;addRecord&lt;/code&gt; object. It will be an array to later keep the records. The next step will be defining a function to save the result of a sum every time it's called.&lt;/p&gt;

&lt;p&gt;We can try defining a new method which has a different name from sum. But that goes against what we're trying to accomplish. The reason that we defined the addRecord in the first place was that we wanted it to be identical to add. The only difference is that when &lt;code&gt;sum&lt;/code&gt; itself is called the result should be recorded. &lt;/p&gt;

&lt;p&gt;For this, we can define a new method in &lt;code&gt;addRecord&lt;/code&gt; prototype and name it sum. It does the job of doing the sum, recording the result and returning it.&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;addRecord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

 &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;records&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;addRecord&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;addRecord&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;second_number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;second_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// add an array which keeps records of current (a, b) and the sum&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should be the complete code of addRecord. Now if we call &lt;code&gt;addRecorder.sum()&lt;/code&gt; it should return the result and also save an array of the current (a,b) values and the sum in the records array.&lt;/p&gt;

&lt;p&gt;You might be asking yourself how is this sum method useful? Why not just rename it in some other way? Or why not just add recording behavior to the original add function?&lt;/p&gt;

&lt;p&gt;The answer actually depends on the design of your application. In our case we want the developers using our code to have access to two different APIs. Both of them are kept really identical. They both take two arguments and they both have the same properties and methods. This will keep the implementation simple for them. Because they won't need to remember two different methods or property names.&lt;/p&gt;

&lt;p&gt;This also keeps the behaviors separated. Maybe the &lt;code&gt;add&lt;/code&gt; object is used for some anonymous behaviors in which we never want any ways of keeping records. So we don't at all implement the behavior in it.&lt;/p&gt;

&lt;p&gt;On the other hand the addRecord function is exactly built for this behavior. It doesn't need too much rewrite and it overrides the sum method, without breaking any functionalities accessible by add.prototype.&lt;/p&gt;

&lt;p&gt;by the way, the thing we did by overriding the &lt;code&gt;sum&lt;/code&gt; method is called &lt;code&gt;Polymorphism&lt;/code&gt;. It means the function does different behaviors in different objects or situations. It was cool, wasn't it? :)&lt;/p&gt;

&lt;p&gt;Here's the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addRecorder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;addRecord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addRecorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 3&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;addRecorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [[1,2,3]]&lt;/span&gt;

&lt;span class="nx"&gt;addRecorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;66&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;addRecorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// 68&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;addRecorder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;records&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// [[1,2,3], [66, 2, 68]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. The End
&lt;/h2&gt;

&lt;p&gt;This has already been a long article. But I have tried to be as through as possible. Because, by learning all of these concepts you actually know what's happening under the hood every time you use the &lt;code&gt;class&lt;/code&gt; keyword. Everything we learned about Inheritance, Polymorphism and Prototypes is exactly how the &lt;code&gt;extends&lt;/code&gt; keyword of classes works.&lt;/p&gt;

&lt;p&gt;Understanding the &lt;code&gt;this&lt;/code&gt; keyword and what value it keeps in different situations can come really handy. Besides those things, knowing how functions are more than just functions, can give you a new insight into how JavaScript is actually shaped.&lt;/p&gt;

&lt;p&gt;JS can be really strange and crazy at times. But don't forget; it's the same tool that has evolved the web so far. And it's capable of evolving even more!&lt;/p&gt;

&lt;p&gt;Thanks for reading this far. And I hope it has been useful to you. If you have any questions, or ideas about what I should write next, reach me in the comments section. I will be glad to answer you!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>node</category>
    </item>
    <item>
      <title>How to hook into the DOM using Vanilla JavaScript!</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Sat, 26 Feb 2022 21:04:08 +0000</pubDate>
      <link>https://dev.to/alimobasheri/how-to-hook-into-the-dom-using-vanilla-javascript-5a6p</link>
      <guid>https://dev.to/alimobasheri/how-to-hook-into-the-dom-using-vanilla-javascript-5a6p</guid>
      <description>&lt;p&gt;An element. A very simple element. It's there. Right in the DOM tree. But we want to hook into it. We want to use simple methods to control what it renders. To control when it updates.&lt;/p&gt;

&lt;p&gt;If you're a web developer, then you might be familiar with React Hooks. I've also written articles on React Hooks Flow. But this is not about them.&lt;/p&gt;

&lt;p&gt;Sure. There's a similarity. They're hooks in React because they let stateless functions use the Class Components abilities like states and lifecycles.&lt;/p&gt;

&lt;p&gt;Here we're going to write logic that saves the value or the state of a DOM element and updates it as the state changes. Then this is not about React. But about an interesting way to interact with the DOM. From pure JavaScript!&lt;/p&gt;




&lt;h2&gt;
  
  
  What are we going to do?
&lt;/h2&gt;

&lt;p&gt;Think of a simple counter app. There are a few elements on the screen to let the user interact with it. &lt;/p&gt;

&lt;p&gt;It displays a big number. Which demonstrates the current count.&lt;/p&gt;

&lt;p&gt;You click a button and it increments the number. Clicking another one results in decrement. The third button lets you reset the counter to zero.&lt;/p&gt;

&lt;p&gt;We're going to create this app. But we're going to do so in a different way. First, we'll write some helper classes to allow us with hooking into the DOM. Then we're going to use them to construct the app logic.&lt;/p&gt;

&lt;p&gt;This is how we're going to use those helper classes:&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;StateHook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;count&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&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;new&lt;/span&gt; &lt;span class="nx"&gt;RenderHook&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;counter&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;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;modify&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;el&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;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&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;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;incrementBtn&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;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;click&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;decrementBtn&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;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;click&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;resetBtn&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;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;click&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;That's it. Of course, we need to write the HTML part, which is short. And we've to create those helper objects.&lt;/p&gt;

&lt;p&gt;This piece of code might seem strange. Even unfamiliar. And that's okay. Because we're going to understand everything step by step.&lt;/p&gt;

&lt;p&gt;In the end, you've got a mini helper library that you can extend or use to create new projects.&lt;/p&gt;

&lt;p&gt;If you're still in doubt whether this article is for you or not, then let me show you what topics it covers.&lt;/p&gt;




&lt;h2&gt;
  
  
  What aspects of JS are we going to work with?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;DOM manipulation. A very simple example of it.&lt;/li&gt;
&lt;li&gt;Classes in JS and their different aspects. Like the public and local properties, inheritance, and chaining.&lt;/li&gt;
&lt;li&gt;The EventTarget instance. This is the main part. To be able to replicate the React Hook Flow order, we have to work with events.&lt;/li&gt;
&lt;li&gt;Understanding how React applications look under the hood.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If these seem interesting to you, let's move along.&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating the project
&lt;/h2&gt;

&lt;p&gt;Only three files. I don't want to waste your time with &lt;code&gt;npm&lt;/code&gt; and &lt;code&gt;CSS styling&lt;/code&gt;. Create a file and name it &lt;code&gt;index.html&lt;/code&gt;. The two other files are scripts. We'll name them: &lt;code&gt;hooks.js&lt;/code&gt; and &lt;code&gt;scripts.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Paste the following boilerplate into &lt;code&gt;index.html&lt;/code&gt;:&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="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&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;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA-Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"IE=edge"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"hooks.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"scripts.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Vanilla Hooks&lt;span class="nt"&gt;&amp;lt;/title&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;main&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&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;"counter"&lt;/span&gt;&lt;span class="nt"&gt;&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;"counter__number"&lt;/span&gt;&lt;span class="nt"&gt;&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;"number"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"counter"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Loading...&lt;span class="nt"&gt;&amp;lt;/p&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;"counter__actions"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"incrementBtn"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"actions__button"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
              + Increment
            &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"decrementBtn"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"actions__button"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
              - Decrement
            &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"resetBtn"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"actions__button"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
              0 Reset
            &lt;span class="nt"&gt;&amp;lt;/button&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&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;/main&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;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This HTML structure creates a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag and three &lt;code&gt;button&lt;/code&gt;s. The &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag handles displaying the counter's current value and each of the buttons has a different role.&lt;/p&gt;

&lt;p&gt;Now let's write some JS code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Hooks
&lt;/h2&gt;

&lt;p&gt;We named one of the hooks files &lt;code&gt;hooks.js&lt;/code&gt;. This is the file where our app's core logic is going to live. We'll write some helper classes. which are able of listening to events and cause updates in the &lt;code&gt;DOM&lt;/code&gt; according to these events.&lt;/p&gt;

&lt;h3&gt;
  
  
  EventTarget
&lt;/h3&gt;

&lt;p&gt;This is how the Mozilla docs explain &lt;code&gt;EventTargets&lt;/code&gt; in JavaScript (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget"&gt;read more here&lt;/a&gt;):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The EventTarget interface is implemented by objects that can receive events and may have listeners for them. In other words, any target of events implements the three methods associated with this interface.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But why do we need to use them?&lt;/p&gt;

&lt;p&gt;An &lt;code&gt;EventTarget&lt;/code&gt; interface allows us to create objects which can dispatch events. This means that in any other part of the code you can attach listeners to the events the &lt;code&gt;EventTarget&lt;/code&gt; dispatches.&lt;/p&gt;

&lt;p&gt;One main parameter in handling &lt;code&gt;DOM&lt;/code&gt; changes is to register specific values as &lt;code&gt;state&lt;/code&gt;s. Whenever these values change, the hooks should reflect them in the DOM.&lt;/p&gt;

&lt;p&gt;Then let's start with writing a state hook.&lt;/p&gt;

&lt;h3&gt;
  
  
  The State Hook
&lt;/h3&gt;

&lt;p&gt;We aim to write a reactive interface for our application. This means that what the hooked elements render in the DOM updates in reaction to changes in our states.&lt;/p&gt;

&lt;p&gt;We're going to use &lt;code&gt;EventTarget&lt;/code&gt;s to write a &lt;code&gt;State class&lt;/code&gt;. This class will hold the state's current value and handle its updates. When we try to change the state value, the class instance will &lt;code&gt;dispatch&lt;/code&gt; an &lt;code&gt;update event&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We attach an &lt;code&gt;eventListener&lt;/code&gt; to the &lt;code&gt;state instance&lt;/code&gt;. And fire callbacks when it dispatches the &lt;code&gt;update event&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Let's write the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;StateHook&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;EventTarget&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;_value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;_value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newValue&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;updateEvent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;CustomEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;update&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dispatchEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updateEvent&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;Let's inspect the code line by line. In the first line, we declare a JS class. We use the &lt;code&gt;extends&lt;/code&gt; keyword to declare that this class inherits from &lt;code&gt;EventTarget&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;This way our &lt;code&gt;State Class&lt;/code&gt;' instances will own the &lt;code&gt;dispatchEvent&lt;/code&gt; and &lt;code&gt;addEventListener&lt;/code&gt; methods. We can use them to handle &lt;code&gt;state change events&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the first line inside the class we define a &lt;code&gt;private instance property&lt;/code&gt; named &lt;code&gt;_value&lt;/code&gt;. When a variable inside a class' enclosing tags starts with the &lt;code&gt;#&lt;/code&gt; character it becomes a private property. This means that the only way to assign its value is from inside the class enclosing tags.&lt;/p&gt;

&lt;p&gt;This property is the one we use to store the state's latest value after each update. We defined it as a private property because we want it to be immutable like &lt;code&gt;React states&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the next line, we write the class &lt;code&gt;constructor&lt;/code&gt;. It only takes one argument which we name &lt;code&gt;value&lt;/code&gt;. This argument is the state's initial value.&lt;/p&gt;

&lt;p&gt;We store the initial value in the class's &lt;code&gt;#_value&lt;/code&gt; property. &lt;/p&gt;

&lt;p&gt;After the constructor we define a &lt;code&gt;get&lt;/code&gt; and a &lt;code&gt;set&lt;/code&gt; method for the &lt;code&gt;#_value&lt;/code&gt; property. We name these methods as &lt;code&gt;value&lt;/code&gt;, so that's the name we'll use later to access them.&lt;/p&gt;

&lt;p&gt;Now we can access the state value by writing &lt;code&gt;instance.value&lt;/code&gt; instead of  &lt;code&gt;instace._value&lt;/code&gt;. The setter method returns null and does nothing. So that we can never write &lt;code&gt;instance._value = x&lt;/code&gt;. Now it's immutable.&lt;/p&gt;

&lt;p&gt;And in the end, we define the &lt;code&gt;update&lt;/code&gt; method for the &lt;code&gt;state&lt;/code&gt; instance. This method takes an argument which we named &lt;code&gt;newValue&lt;/code&gt;. We assign this argument's value to the state's private 'value' property.&lt;/p&gt;

&lt;p&gt;Then by writing &lt;code&gt;const updateEvent = new CustomEvent("update")&lt;/code&gt; we create a custom event with the key 'update'. Custom events are like every other event. They take a name from you, and any &lt;code&gt;Event Target&lt;/code&gt; can dispatch them.&lt;/p&gt;

&lt;p&gt;In the last line of this method, we dispatch this event. Now we can attach listeners to the instances of this state. And make changes in the &lt;code&gt;DOM&lt;/code&gt; using the new state value.&lt;/p&gt;

&lt;p&gt;Then let's write the second hook. Which controls what the &lt;code&gt;DOM&lt;/code&gt; renders, by listening to the state hook.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Render Hook
&lt;/h3&gt;

&lt;p&gt;This hook has one simple task. We give it a function by which it can find a specific element. Then we give it specific &lt;code&gt;state&lt;/code&gt;s which it can listen to their updates. Finally, it gets a function that we call &lt;code&gt;modifier&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It calls the modifier the first time the &lt;code&gt;DOM&lt;/code&gt; is ready and then each time the states' values change. It is the hook's task to keep track of the states and call the modifier when they change.&lt;/p&gt;

&lt;p&gt;The modifier is a function that the hook calls every time the state changes. So we can use it to control what the element renders.&lt;/p&gt;

&lt;p&gt;This is the how we can write it:&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;class&lt;/span&gt; &lt;span class="nx"&gt;RenderHook&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_getElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;getElement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_modifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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="nx"&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;load&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;update&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;e&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;modify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;modifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_modifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;modifier&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;render&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;theElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_getElement&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;theElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_modifier&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_modifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;theElement&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;&lt;code&gt;RenderHook&lt;/code&gt; is a simple class. It doesn't inherit from &lt;code&gt;EventTarget&lt;/code&gt;. Because we have no need for dispatching events from its instances.&lt;/p&gt;

&lt;p&gt;It only takes a function as an argument and assigns its value to the &lt;code&gt;_getElement&lt;/code&gt; property. Calling this function should return a &lt;code&gt;DOM Element&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the next line, we define the &lt;code&gt;_modifier&lt;/code&gt; property which has an initial null value. It will hold the modifier function which can be set later using a method.&lt;/p&gt;

&lt;p&gt;At the end of the constructor, we add a listener to &lt;code&gt;window's load event&lt;/code&gt;. The instance's render method will run for the first time as soon as the &lt;code&gt;DOM&lt;/code&gt; is loaded.&lt;/p&gt;

&lt;p&gt;After the constructor, we define a &lt;code&gt;use&lt;/code&gt; method. It accepts a &lt;code&gt;state&lt;/code&gt; argument. The argument should be an instance of the &lt;code&gt;StateHook&lt;/code&gt; class. Then we add a listener to its &lt;code&gt;update&lt;/code&gt; event. Each time a state updates it calls the instace's &lt;code&gt;render&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;At the end of this method, we return &lt;code&gt;this&lt;/code&gt;. You might wonder why we do so. This way we're returning the current instance. This benefits us while calling this class' methods as we can use &lt;code&gt;chaining&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Chaining is a more declarative way of calling an instance's methods. To see the difference, look at the following example. It tries to add three different states to a &lt;code&gt;RenderHook&lt;/code&gt; instance:&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;counterRender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;RenderHook&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;counter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;counterRender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counterState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;counterRender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;counterRender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;styleState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code can be shorter and more concise by using chaining. Each time we call the &lt;code&gt;use&lt;/code&gt; method it returns us a &lt;code&gt;RenderHook&lt;/code&gt; instance. So we can attach each method call to the previous one. Resulting in the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;RenderHook&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;counter&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;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counterState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;styleState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our code looks clean ;)&lt;/p&gt;

&lt;p&gt;Next comes the modify method. It takes a function. And assigns it to the current &lt;code&gt;instance's&lt;/code&gt; &lt;code&gt;_modifier&lt;/code&gt; &lt;code&gt;property&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And the last method in the line is &lt;code&gt;render&lt;/code&gt;. It's the base of this concept. It's the promised one. The one who does the final job.&lt;/p&gt;

&lt;p&gt;You give it no arguments. Call it and it will proceed to update the &lt;code&gt;DOM&lt;/code&gt;. To do so it uses what data you have provided using the other methods. &lt;/p&gt;

&lt;p&gt;First it calls the &lt;code&gt;_getElement&lt;/code&gt; function. Then assigns the returned value to &lt;code&gt;theElement&lt;/code&gt; variable. Then it checks if &lt;code&gt;theElement&lt;/code&gt; is not &lt;code&gt;nullish&lt;/code&gt;. That can happen in case the element has been removed from the &lt;code&gt;DOM&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;It calls the &lt;code&gt;_modifier&lt;/code&gt; function and passes &lt;code&gt;theElement&lt;/code&gt; to it. And the modifier can proceed to do its job. Which could be updating the &lt;code&gt;DOM&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And that's all!&lt;/p&gt;




&lt;h2&gt;
  
  
  How it works.
&lt;/h2&gt;

&lt;p&gt;Once more let's look at the final code I showed you at the beginning:&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;StateHook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;count&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&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;new&lt;/span&gt; &lt;span class="nx"&gt;RenderHook&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;counter&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;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;modify&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;el&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;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&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;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;incrementBtn&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;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;click&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;decrementBtn&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;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;click&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;resetBtn&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;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;click&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now it shouldn't seem confusing anymore. We define a state using the &lt;code&gt;StateHook&lt;/code&gt;. Its initial value is 0. Then we create a &lt;code&gt;RenderHook&lt;/code&gt;. We pass it the function to get the counter text element.&lt;/p&gt;

&lt;p&gt;We tell it to use the &lt;code&gt;counter state&lt;/code&gt; and start listening to its updates. And we give it a &lt;code&gt;modifier&lt;/code&gt; which it should call each time the counter state is updated.&lt;/p&gt;

&lt;p&gt;In the next three lines, we use simple JavaScript. We find the button elements in the &lt;code&gt;DOM&lt;/code&gt; and attach listeners to them. Clicking the increment button increments the &lt;code&gt;count state&lt;/code&gt;'s value using its &lt;code&gt;update&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;We configure the two other buttons in a similar way.&lt;/p&gt;

&lt;p&gt;Every time we call the &lt;code&gt;state's&lt;/code&gt; update method it dispatches a &lt;code&gt;Custom Event&lt;/code&gt;. This event's name is &lt;code&gt;update&lt;/code&gt;. This dispatch invokes our &lt;code&gt;RenderHook's&lt;/code&gt; render method. And in the end, our &lt;code&gt;modifier&lt;/code&gt; updates the &lt;code&gt;text element's innerText&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The End.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Cover photo by Vishal Jadhav on unsplash.)&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Story Of React Effects.</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Sun, 05 Dec 2021 20:36:32 +0000</pubDate>
      <link>https://dev.to/alimobasheri/a-story-of-react-effects-4dia</link>
      <guid>https://dev.to/alimobasheri/a-story-of-react-effects-4dia</guid>
      <description>&lt;p&gt;We use states to update what components render. But what if some operations don’t cause immediate changes in the component’s output? &lt;/p&gt;

&lt;p&gt;The React documentation calls them side effects. And some examples include using web APIs, handling HTTP requests, and attaching listeners to events.&lt;/p&gt;

&lt;p&gt;For handling these, React provides us with the &lt;code&gt;useEffect&lt;/code&gt; hook. It calls the function you pass it each time the component renders or updates. Through this article, we’ll learn why React implements this functionality. &lt;/p&gt;

&lt;p&gt;And when you finish reading this you can start to use effects to your profit!&lt;/p&gt;




&lt;h2&gt;
  
  
  Side effects
&lt;/h2&gt;

&lt;p&gt;Every action ends in an outcome. And every component renders an output. To lose weight, you need to burn fat. It’s possible by going on a diet and beginning exercises. &lt;/p&gt;

&lt;p&gt;But get ready for the side effects.&lt;/p&gt;

&lt;p&gt;These side effects include headaches, fatigue and, dizziness. They are so because they are not what you expect. Neither are they a part of the main routine.&lt;/p&gt;

&lt;p&gt;When exercising exhausts you, you need to rest for a while. You need to gain energy before you can return to your everyday life.&lt;/p&gt;

&lt;p&gt;In a React app, when requesting data from the server, we can face the same problem. Because we're not sure how long it can take to retrieve the data from the server. And we're not sure if we can render it in the component's output.&lt;/p&gt;

&lt;p&gt;So we call the &lt;code&gt;useEffect&lt;/code&gt; hook to start fetching the needed data from the server. In the meanwhile, the component renders a placeholder output. You know what it is.&lt;/p&gt;

&lt;p&gt;A beautiful loading indicator!&lt;/p&gt;




&lt;h2&gt;
  
  
  The useEffect Hook
&lt;/h2&gt;

&lt;p&gt;From the React documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By using this Hook, you tell React that your component needs to do something after render. React will remember the function you passed (we’ll refer to it as our “effect”), and call it later after performing the DOM updates.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So you pass a function to &lt;code&gt;useEffect&lt;/code&gt;, and it remembers to call it after each render or update. This means that by the time the component calls your effect, it has already updated the state. And the browser has finished painting the screen.&lt;/p&gt;

&lt;p&gt;But why do we need to wait for the render to finish before running the effect?&lt;/p&gt;

&lt;p&gt;We can find the answer.&lt;/p&gt;




&lt;h2&gt;
  
  
  The variety of side effect
&lt;/h2&gt;

&lt;p&gt;Think of a big drapery shop. Full of luxurious and colorful textiles. Rows over rows of fabrics. Silks from all over the world. A great show of human creativity.&lt;/p&gt;

&lt;p&gt;Now imagine the customers. A market welcomed by women. Waves of them coming to the store and leaving hourly. Think of how the owners can manage the purchases and the sales.&lt;/p&gt;

&lt;p&gt;Then there's one special case. One of the customers, a young blonde lady, is looking for a rare type of silk. &lt;/p&gt;

&lt;p&gt;It was in stock three days ago. But now, all of it was sold out.&lt;/p&gt;

&lt;p&gt;So the salesman takes the lady's phone number. Then he makes phone calls with some well-known silk traders to order a new set of that special silk.&lt;/p&gt;

&lt;p&gt;In the end, he informs the lady that he'll call her later as soon as the product is available. (And let's hope he doesn't text her: "How you doin?"!)&lt;/p&gt;

&lt;p&gt;A drapery shop could run out of any of its products at any time. Do you think the manager should not open his store if some of his assets are missing? &lt;/p&gt;

&lt;p&gt;Does the above question make any sense to you? By waiting for the perfect time, he may not as well make more than ten sales per week.&lt;/p&gt;

&lt;p&gt;A React component is like this drapery shop. It renders what it has in stock. And if data isn't currently available in the state, it renders a loading indicator. Then tries to fetch the data in a &lt;code&gt;useEffect&lt;/code&gt; hook.&lt;/p&gt;

&lt;p&gt;A code example looks as following:&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;Drapery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://example.com/endpoint&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;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;setIsLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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;error&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="nx"&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;There was an error while fetching data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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;return&lt;/span&gt; &lt;span class="nx"&gt;isLoading&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Spinner&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ListComponent&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;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 code renders a component with two states. &lt;/p&gt;

&lt;p&gt;One of them is &lt;code&gt;loading&lt;/code&gt;. In case its value is true, it means data isn't available. So the component should render a loading indicator. The other one is &lt;code&gt;data&lt;/code&gt; which holds a list of drapery items. And &lt;code&gt;ListComponent&lt;/code&gt; is going to render them.&lt;/p&gt;

&lt;p&gt;We should focus on the &lt;code&gt;useEffect&lt;/code&gt; part. The hook accepts two arguments; a function and a dependency array. &lt;/p&gt;

&lt;p&gt;The function holds the logic of your effect.&lt;/p&gt;

&lt;p&gt;Using the dependency array, you pass a set of values that &lt;code&gt;React&lt;/code&gt; will start to track. Anytime any of the values in this array changes, React calls your effect.&lt;/p&gt;

&lt;p&gt;In this case, we've passed an empty array as a dependency. This indicates that React calls this effect only once. That is after the initial render.&lt;/p&gt;

&lt;p&gt;If you pass no dependency array to the hook, React calls the effect after each render or update.&lt;/p&gt;

&lt;p&gt;The function we passed to &lt;code&gt;useEffect&lt;/code&gt; tries to start fetching data from the server. &lt;code&gt;JS fetch API&lt;/code&gt; returns a promise. We use it to update the states as soon as we get the response.&lt;/p&gt;

&lt;p&gt;We update the &lt;code&gt;data&lt;/code&gt; state with what we get from the server. Then we set the &lt;code&gt;loading&lt;/code&gt; state to &lt;code&gt;false&lt;/code&gt;. Then the condition in our component's &lt;code&gt;return&lt;/code&gt; expression evaluates to &lt;code&gt;false&lt;/code&gt;. That’s when it renders the data using &lt;code&gt;ListComponent&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;While our &lt;code&gt;effect&lt;/code&gt; causes a re-render by updating states, it doesn't affect the render. It runs in parallel. And it never causes any direct changes in what the component renders.&lt;/p&gt;

&lt;p&gt;In the previous section we had a question. Why should effects run after the render is updated?&lt;/p&gt;

&lt;p&gt;In this case, a good reason is that network calls can affect the client's performance. We need to show the loading indicator as soon as possible. &lt;/p&gt;

&lt;p&gt;In web apps, each second lost due to performance is thousands of potential users lost forever:(&lt;/p&gt;




&lt;h2&gt;
  
  
  How much did we make today?
&lt;/h2&gt;

&lt;p&gt;A drapery shop's manager needs to keep its daily sales data in a secure place. He needs to have a record of everything they buy and sell daily.&lt;/p&gt;

&lt;p&gt;They save pieces of information on every new asset they deliver and sell daily. They should do it as soon as any change occurs.&lt;/p&gt;

&lt;p&gt;While writing React web apps we sometimes need to save the user's data in the client's storage. The &lt;code&gt;localStorage API&lt;/code&gt; is a good option for this case.&lt;/p&gt;

&lt;p&gt;We can use the &lt;code&gt;useEffect&lt;/code&gt; hook to listen to changes in state as soon as it is updated. Then we can set a JSON clone of the new state in the client's storage.&lt;/p&gt;

&lt;p&gt;An 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;UserContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createContext&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;UserProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;children&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;persistedUserData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;persistedUserData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;persistedUserData&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="nx"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jsonUserData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;jsonUserData&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt;&lt;span class="nx"&gt;user&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;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/UserContext.Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;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 the above example, we are using &lt;code&gt;React.createContext&lt;/code&gt; to initialize a context. If you don't know about &lt;code&gt;context&lt;/code&gt;, you can think of it as an API. It makes the value you pass to its &lt;code&gt;Provider&lt;/code&gt; available to every component in its tree. To learn more you can read the &lt;a href="https://reactjs.org/docs/context.html"&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Then in the body of the &lt;code&gt;Provider&lt;/code&gt;, we have used three hooks. One is a &lt;code&gt;useState&lt;/code&gt; which holds user info. Its initial value is null, which means no user data is available.&lt;/p&gt;

&lt;p&gt;The two other hooks are both &lt;code&gt;useEffects&lt;/code&gt;. The first effect runs only after the first render, as its dependency array is empty. We use it to check if there's any user data previously stored in the client's storage. If so, we parse the &lt;code&gt;JSON&lt;/code&gt; data as a &lt;code&gt;JS&lt;/code&gt; object and update our state with it.&lt;/p&gt;

&lt;p&gt;The second hook has a dependency array with one element: &lt;code&gt;user&lt;/code&gt; state value. So it will run every time we update the user state. This effect makes a &lt;code&gt;JSON&lt;/code&gt; copy of the &lt;code&gt;user&lt;/code&gt; state and saves it to the client's &lt;code&gt;localStorage&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second &lt;code&gt;useEffect&lt;/code&gt; is a good example of when we should use effects. We update the client's localStorage, which causes no change in the render. There's no place for its logic in the component body.&lt;/p&gt;

&lt;p&gt;We've also learned how we can control when effects are run by setting their dependency arrays.&lt;/p&gt;

&lt;p&gt;Good!&lt;/p&gt;




&lt;h2&gt;
  
  
  Did a fire catch here?
&lt;/h2&gt;

&lt;p&gt;Silks, clothes, curtains. They're all so soft and lovely. Yet they're so vulnerable to ignition. A small flame can turn the whole drapery into ashes. &lt;/p&gt;

&lt;p&gt;Piles of money, into ash, that is.&lt;/p&gt;

&lt;p&gt;A drapery shop's manager is aware of this hazard and takes every precaution step needed to prevent its occurrence. But there's always a chance of a mistake. And a mistake is enough to undo all the cautions.&lt;/p&gt;

&lt;p&gt;So he has specified an explicit procedure for when fire catches in the store. And he has instructed it to his employees. In addition, he has printed them on paper and stuck them on the wall.&lt;/p&gt;

&lt;p&gt;There's little chance of things going wrong, but it might happen at any time. And he needs to bear it in mind.&lt;/p&gt;

&lt;p&gt;Do you need a similar web app example? A text editor is one.&lt;/p&gt;

&lt;p&gt;When working with a text editor, you need to save your changes to not lose them when you reopen the text. Now there's a chance you forget you haven't saved your changes and click the browser's close button.&lt;/p&gt;

&lt;p&gt;A good web application is going to prevent this disaster happen. It stops the navigator from closing the tab and alerts you that you haven’t saved your work.&lt;/p&gt;

&lt;p&gt;Sir or madam, truly your forgiveness I implore, are you sure you want to continue?&lt;/p&gt;

&lt;p&gt;To implement this feature in a React app we can do it with &lt;code&gt;useEffect&lt;/code&gt; like the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Editor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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;useEffect&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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;beforeunload&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;showDialog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;beforeunload&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;EditorPanel&lt;/span&gt;&lt;span class="o"&gt;/&amp;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 the above example, we attach a listener to &lt;code&gt;window&lt;/code&gt;'s &lt;code&gt;beforeunload&lt;/code&gt; event. This event is fired whenever the user tries to close the browser or the current tab.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook is a good choice in this case because the component is already rendered. Then we're sure that the window object is loaded and we can attach an &lt;code&gt;event listener&lt;/code&gt; to it.&lt;/p&gt;

&lt;p&gt;Also, we've used a &lt;code&gt;return&lt;/code&gt; statement in the &lt;code&gt;useEffect&lt;/code&gt; hook. You can always return a function in your effect. React will call this function when the component is unmounted.&lt;/p&gt;

&lt;p&gt;We returned a function that removes the event listener attached to the window because we don't want to prevent the user from exiting the app when there is no editor open.&lt;/p&gt;

&lt;p&gt;A function you return in your effects is called &lt;code&gt;cleanup&lt;/code&gt; function. Since effects like this listener are out of the component's scope and are not related to its render, it's always a good idea to write a cleanup function for them.&lt;/p&gt;

&lt;p&gt;When a drapery shop's manager goes broke and has to leave his place, he also has to clean the wall from his emergency instructions. The new manager won't like to see them there!&lt;/p&gt;




&lt;h2&gt;
  
  
  That's it!
&lt;/h2&gt;

&lt;p&gt;In this article, we learned why we need to separate effect logic from render and how to do it wisely. We also learned different use cases of this feature and how to handle them correctly.&lt;/p&gt;

&lt;p&gt;I hope you liked it. &lt;/p&gt;

&lt;p&gt;One last word. If you were in doubt whether you should write an effect or just bring the logic inside the component's body, ask yourself this question: Is the operation immediately going to change what the component renders?&lt;/p&gt;




&lt;p&gt;In case this article was of any help to you, I will be glad if you follow me on Twitter or mention me in an appreciation tweet. That means a lot to me!&lt;br&gt;
&lt;a href="https://twitter.com/MobasheriMirali?t=43ePoR2pDXS0U6JqKbzw5Q&amp;amp;s=09"&gt;@MobasheriMirali&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Story Of React States.</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Sat, 27 Nov 2021 20:13:09 +0000</pubDate>
      <link>https://dev.to/alimobasheri/a-story-of-react-states-5di4</link>
      <guid>https://dev.to/alimobasheri/a-story-of-react-states-5di4</guid>
      <description>&lt;p&gt;It's hard to maintain a state. Harder to maintain a UI. And near impossible to keep a user happy.&lt;/p&gt;

&lt;p&gt;No matter how well you plan, disintegration always happens.&lt;/p&gt;

&lt;p&gt;React is a well-known web UI library with a long history of state implementation. Components shape the application. But it's a state's job to keep it inside the sanity borders.&lt;/p&gt;

&lt;p&gt;That is unless it drives the developer insane before achieving this goal!&lt;/p&gt;




&lt;h2&gt;
  
  
  Different seasons, different states.
&lt;/h2&gt;

&lt;p&gt;We know how the seasons pass. In spring, flowers bloom. In summer, sweats run down the armpits. With autumn, leaves leave the branches. And who will forget the snowy days of winter? (Hint: People who live after global warming makes its debut!)&lt;/p&gt;

&lt;p&gt;Nature can resemble a big &lt;a href="https://reactjs.org/docs/components-and-props.html#function-and-class-components"&gt;&lt;code&gt;React Component&lt;/code&gt;&lt;/a&gt;. A part of life on Earth. But it goes through various phases. We know each as a season.&lt;/p&gt;

&lt;p&gt;Different parts of a front-end application can be in different phases too. They are the results of changes in what we call a &lt;code&gt;state&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  An introduction to our study.
&lt;/h2&gt;

&lt;p&gt;Imagine a man with a bad temper. He swears in hot summers. And beats people when it gets cold enough. No one can predict him.&lt;/p&gt;

&lt;p&gt;You might say that he is affected by the weather.&lt;/p&gt;

&lt;p&gt;For example, he is a kind man during springs and even sings poems when it rains. Winters are cold and make him impatient. That is why he will prefer to get his deals done by action rather than talks.&lt;/p&gt;

&lt;p&gt;Such a man lacks good state management, making him unable to control his behavior. A similar issue can exist in a front-end application too. That’s when it gives inappropriate responses or gives none at all.&lt;/p&gt;

&lt;p&gt;Then, let's have a quick review of what a state is in a React Component.&lt;/p&gt;




&lt;h2&gt;
  
  
  React Components State.
&lt;/h2&gt;

&lt;p&gt;From the &lt;a href="https://reactjs.org/docs/state-and-lifecycle.html"&gt;React documentation&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;State is similar to props, but it is private and fully controlled by the component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;Props&lt;/code&gt; themselves are &lt;code&gt;Objects&lt;/code&gt;. Their &lt;code&gt;keys&lt;/code&gt; are the name of the &lt;code&gt;attributes&lt;/code&gt; we pass to the component through &lt;code&gt;JSX&lt;/code&gt;. And their &lt;code&gt;values&lt;/code&gt; are that of the corresponding attributes.&lt;/p&gt;

&lt;p&gt;While a state can be an attribute or a collection of them, it can be an &lt;code&gt;Object&lt;/code&gt;, a &lt;code&gt;String&lt;/code&gt;, or any other &lt;code&gt;JavaScript Type&lt;/code&gt;. But what is the main difference between a state and a &lt;code&gt;prop&lt;/code&gt;? &lt;/p&gt;

&lt;p&gt;Answer: The &lt;code&gt;component&lt;/code&gt; receives &lt;code&gt;props&lt;/code&gt; from its parent, while it creates and &lt;code&gt;controls&lt;/code&gt; its own &lt;code&gt;state&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is the actual problem?
&lt;/h2&gt;

&lt;p&gt;At this point we can ask a question: What makes a great application? &lt;/p&gt;

&lt;p&gt;Perhaps dedication to its end. The desire to deliver satisfaction. A sense of being useful. What we may call confidence. &lt;/p&gt;

&lt;p&gt;A bad application isn't pushed by these things. It already feels satisfied and confident. It is directed by more solid things. It is affected by data flows. By a powerful inertia.&lt;/p&gt;

&lt;p&gt;It's uncareful about user expectations. And it's rarely suited for different user stories. It only works if everything is ok. Which is seldom the case in a real world application.&lt;/p&gt;




&lt;h2&gt;
  
  
  Can we make a state using a simple variable?
&lt;/h2&gt;

&lt;p&gt;Let's say that we've a component called &lt;code&gt;Nature&lt;/code&gt;. This component has a main state variable: &lt;code&gt;season&lt;/code&gt;. First let's declare it just using a simple &lt;code&gt;JavaScript variable&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Nature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;season&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;spring&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Currently&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;season&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;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 line &lt;code&gt;const season = 'spring';&lt;/code&gt; is where we declared our state variable. The output HTML looks like this:&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;p&amp;gt;&lt;/span&gt;Currently it is spring&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's add a button to our function's return. We pass its &lt;code&gt;onClick&lt;/code&gt; event a callback which will try to change the variable &lt;code&gt;season&lt;/code&gt;'s value to 'summer':&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Nature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;season&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;spring&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;changeSeason&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;season&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;summer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Currently&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;season&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;changeSeason&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;Click&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;change&lt;/span&gt; &lt;span class="nx"&gt;season&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;It will output the previous result with an added button. Yet if you click on the button, it won't change the season's name in the output. &lt;/p&gt;

&lt;p&gt;The variable's value changes to summer but the &lt;code&gt;onClick&lt;/code&gt; will never result in a change in the output.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;To answer that we should learn about &lt;code&gt;React Hooks Flow&lt;/code&gt;. This is where things get interesting.&lt;/p&gt;




&lt;h2&gt;
  
  
  Stories of a component's madness.
&lt;/h2&gt;

&lt;p&gt;Events. A sequence of them shapes what we may know as a storyline. For example, you may have just graduated from high school. This is an important event for you that also describes your level of education.&lt;/p&gt;

&lt;p&gt;Our memories and personalities are made of events. Our resumes are fully contented by them. And to get our life move on, we expect future events too.&lt;/p&gt;

&lt;p&gt;A Component's lifecycle is full of events. We're going to have a look at an example.&lt;/p&gt;

&lt;p&gt;Assume a submit button which is used to register user information. The button is disabled until the user fills the required fields correctly. The form component uses a state variable to enable the button.&lt;/p&gt;

&lt;p&gt;It works just fine. The user fills in the form. The gray button turns blue and the user clicks on it. In the background the API request for registering is sent. But the user is not informed. They don't see any loading element or message.&lt;/p&gt;

&lt;p&gt;They think it hasn't worked and they click on it again. And guess what! Another request is sent in the background. The first request is successful to register the user. &lt;br&gt;
The component is designed to redirect the user after success. But...&lt;/p&gt;

&lt;p&gt;The response to the second request comes in a hurry. Acknowledging the component that the user's email is a duplicate. Now another state variable holding the email duplication error, turns true.&lt;/p&gt;

&lt;p&gt;The component gives the user an error informing that the email is a duplication and the redirect just doesn't work because the error state is not false. The component has been instructed not to redirect if the error state is not clear. Which is irrelevant. And is a bad state management.&lt;/p&gt;

&lt;p&gt;We're faced with several events in this example. One of them happens when the form is filled. Another occurs when the user clicks on the button. The last event is when the response has arrived.&lt;/p&gt;

&lt;p&gt;We as users and developers can understand these events. But a UI library such as React is not as smart as a human. It has to be instructed what the events are, and it has to register them beforehand.&lt;/p&gt;

&lt;p&gt;That's what we call &lt;code&gt;Components Hook Flow&lt;/code&gt;.&lt;/p&gt;


&lt;h2&gt;
  
  
  React Components Hook Flow
&lt;/h2&gt;

&lt;p&gt;Hooks were added to React from version 16.8. They were a major update at the time, as they empowered the stateless function components with abilities that were only available in Class Components.&lt;/p&gt;

&lt;p&gt;It was a good thing to have the ability to run effects and change states in a functional component. But a Class Component also provided the developers with Lifecycle Methods, eg. componentDidMount or shouldComponentUpdate. &lt;/p&gt;

&lt;p&gt;The functional components and hooks don't provide us with Lifecycle Methods. Instead different hooks in a functional component are run in a specific order so that the developer can implement the same Lifecycle Logics by considering this order.&lt;/p&gt;

&lt;p&gt;The Hooks Flow is made of 3 main stages: &lt;code&gt;Mount&lt;/code&gt;, &lt;code&gt;Update&lt;/code&gt; and &lt;code&gt;Unmount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When a component mounts for the first time, its initial values are set. These include the &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useReducer&lt;/code&gt; initializer functions. Then it will continue the rest of the codes you have added in your functional component, up until it reaches the returned value. &lt;br&gt;
Then before rendering the returned JSX, it will run your &lt;code&gt;layoutEffects&lt;/code&gt; which you create using the &lt;code&gt;useLayoutEffect&lt;/code&gt; hook. Then the browser paints the screen to reflect the React Virtual DOM. Then the &lt;code&gt;effects&lt;/code&gt; which you have registered using &lt;code&gt;useEffect&lt;/code&gt; are called.&lt;/p&gt;

&lt;p&gt;It's a straightforward flow for mounting the component. But then the component will need to update. This can happen due to two reasons: either there's a change in props or a state has been updated.&lt;/p&gt;

&lt;p&gt;The updating stage has its own steps: It will run your functional component and update the DOM based on the new changes resulting from the updated state.&lt;br&gt;
In the next step, it's going to clear the previous layoutEffects, afterward, it will run the layoutEffects. The browser repaints the screen to reflect the changes. &lt;br&gt;
And finally, before running the effects, react is going to clean the previous effects.&lt;/p&gt;

&lt;p&gt;The updating stage has a similar order of steps to the mounting one. It only differs in the details. Like, the mounting stage uses initial state values, and the updating stage uses the new ones. The mounting stage runs the effects but the updating one first will try to clear the effects caused by previous updating or mounting stages of the component.&lt;/p&gt;

&lt;p&gt;The third stage in this flow as we mentioned is the unmounting stage. In this stage the whole component will be cleared from the screen. So nothing is going to happen, except that React will try to clear any remaining layoutEffects and effects.&lt;/p&gt;

&lt;p&gt;Now that we know the React Hooks Flow, we can realize why storing our Component's state inside a simple variable won't cause a change in the DOM. &lt;/p&gt;

&lt;p&gt;Because React won't update the DOM, unless it is sure that something has changed.&lt;/p&gt;

&lt;p&gt;The way React listens to the state changes is like how we can add eventListeners in JS. For example, assume a text input element. We can add listeners for its &lt;code&gt;value change&lt;/code&gt;, or when the input is &lt;code&gt;blurred&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;React adds listeners to the state variables' changes. And when we call the stateSetter functions, this event is fired, and then React knows how to update the DOM.&lt;/p&gt;

&lt;p&gt;Then, let's rewrite our previous code to make it work.&lt;/p&gt;


&lt;h2&gt;
  
  
  The right way to declare a state
&lt;/h2&gt;

&lt;p&gt;React provides us with the &lt;code&gt;useState&lt;/code&gt; hook to bring states to functional components. To initialize a state you need to call the hook and pass it the state's &lt;code&gt;initial value&lt;/code&gt;.&lt;br&gt;
The hook will return an array of two elements. The first element in the array is the &lt;code&gt;state's value&lt;/code&gt; and the second element is the &lt;code&gt;state-setter function&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We'll use this function to add the season state to our component. We'll also rewrite our &lt;code&gt;changeSeason&lt;/code&gt; function to use the &lt;code&gt;state-setter&lt;/code&gt; function returned by &lt;code&gt;useState&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Nature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;season&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSeason&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;spring&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;changeSeason&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setSeason&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;summer&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="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Currently&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;season&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;changeSeason&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;Click&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;change&lt;/span&gt; &lt;span class="nx"&gt;season&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;Now after rendering this component if we try to click on the button we'll see the season name in the paragraph changes to &lt;code&gt;summer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the above code, the variable &lt;code&gt;season&lt;/code&gt; refers to the first element returned by useState which holds the latest state value. And setSeason is the method we can use to update the state's value.&lt;/p&gt;

&lt;p&gt;We learned how we can bring states to a functional component in React. And we've also learned how states work in a more fundamental way.&lt;/p&gt;

&lt;p&gt;Knowing the right way to declare a state, a great question pops up: Is there also a right way to use states in React?&lt;/p&gt;




&lt;h2&gt;
  
  
  Life is a journey (So is React development)
&lt;/h2&gt;

&lt;p&gt;No one is like anyone else. No one shares the exact same interests as anyone else. We're all unique through our preferences. And this uniqueness also affects the way we live our lives.&lt;/p&gt;

&lt;p&gt;Different React projects also have their own differences. They differ in how they manage their states, their effects, their components trees, or even their folder structures.&lt;/p&gt;

&lt;p&gt;No one state that you should structure a React project in a specific way. You have to understand the underlying mindset which React uses for managing its re-renders, props, states, effects and etc.&lt;/p&gt;

&lt;p&gt;Throughout this article, we learned about the mindset behind how React implements states. I hope it can help you to better understand what they are and why they matter.&lt;/p&gt;

&lt;p&gt;This is already a long article and I'll end it here as I don't want to bore you with extra information. But let me add one more tip. Anytime you were in doubt whether you should use a state or not, try to answer this question: Should changes in this variable cause a re-render?&lt;/p&gt;

&lt;p&gt;Credits for cover image: M. Schuppich/Shutterstock.&lt;/p&gt;




&lt;p&gt;I have no content creation experience in English Twitter yet. But I'd like to start tweeting about different subjects of programming in my Twitter account. I'd really be thankful of your support if you start following my twitter account:)&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Story Behind JavaScript Promises - A Use Case Based Approach!</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Sat, 07 Aug 2021 16:35:44 +0000</pubDate>
      <link>https://dev.to/alimobasheri/the-story-behind-javascript-promises-a-use-case-based-approach-5bdd</link>
      <guid>https://dev.to/alimobasheri/the-story-behind-javascript-promises-a-use-case-based-approach-5bdd</guid>
      <description>&lt;h2&gt;
  
  
  Table Of Contents:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;❓ Why should you read this?&lt;/li&gt;
&lt;li&gt;🏇 In a hurry?&lt;/li&gt;
&lt;li&gt;🤝 How Promises Are Made: Please Put A Ticket!&lt;/li&gt;
&lt;li&gt;&lt;a href="//#Main%20Usages%20Of%20A%20Promise:%20Making%20API%20Calls."&gt;🔛 Main Usages Of A Promise: Making API Calls.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="//#Callbacks:%20The%20Green%20And%20The%20Red%20Button."&gt;✅❌ Callbacks: The Green And The Red Button.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="//#Callbacks:%20Then%20&amp;amp;%20Catch."&gt;↩️↪️ Callbacks: Then &amp;amp; Catch.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;⛓️ The Methodology Behind Promises: Returning Objects!&lt;/li&gt;
&lt;li&gt;&lt;a href="//#Static%20Methods:%20%20raw%20%60Promise.all%60%20endraw%20%20&amp;amp;%20%20raw%20%60Promise.allSettled%60%20endraw%20"&gt;🕸️ Static Methods: &lt;code&gt;Promise.all&lt;/code&gt; &amp;amp; &lt;code&gt;Promise.allSettled&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="//#Static%20Methods:%20%20raw%20%60Promise.any%60%20endraw%20%20&amp;amp;%20%20raw%20%60Promise.race%60%20endraw%20."&gt;🏃🏻‍♂️ Static Methods: &lt;code&gt;Promise.any&lt;/code&gt; &amp;amp; &lt;code&gt;Promise.race&lt;/code&gt;.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="//#Promise%20Callbacks:%20%20raw%20%60finally%60%20endraw%20."&gt;🔚 Promise Callbacks: &lt;code&gt;finally&lt;/code&gt;.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;The young librarian felt satisfied with what she did. Sarah had started her job when she was only 22 years old. Having known her as a long-time member of the library, the kind Mrs. Perkins hired her. &lt;/p&gt;

&lt;p&gt;Mrs. Perkins had been the library's manager for as long as 30 years.&lt;/p&gt;

&lt;p&gt;Now Sarah was the main person responsible for the place. She did a great job. People liked how she treated them and controlled everything.&lt;/p&gt;

&lt;p&gt;But it didn't take long before things got out of control. &lt;/p&gt;

&lt;p&gt;Mrs. Perkins retired. The girl had to do everything alone. And a new wave of readers was joining the library. She couldn't deliver services anymore. &lt;/p&gt;

&lt;p&gt;She was forced to make &lt;strong&gt;promises&lt;/strong&gt;...&lt;/p&gt;




&lt;h2&gt;
  
  
  ❓ Why should you read this?&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;This might seem a long article. And the story might look unrelated. &lt;/p&gt;

&lt;p&gt;But let's face the truth. How many times have you learned a concept but have never used it? &lt;br&gt;
Perhaps there were situations where you could use &lt;code&gt;Promise.race&lt;/code&gt;. But you didn't. Because despite you knew what it did, you weren't sure what its use case could be. &lt;/p&gt;

&lt;p&gt;And when you got into a problem which you could solve using &lt;code&gt;Promice.race&lt;/code&gt;, you chose to go your own way. Because you weren't even sure if it was useful there.&lt;/p&gt;

&lt;p&gt;You knew what it did. But you didn't know its use case.&lt;/p&gt;

&lt;p&gt;Here, I'm presenting you with a story. It also teaches you about different ways you can use &lt;code&gt;Promises&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But it also tries to symbolize its use cases through a real-world story. Next time you face a similar situation you'll be quick to know how to handle it. And if not, you have at least read a story!😉&lt;/p&gt;

&lt;p&gt;Enough talk. I have promises to keep.&lt;/p&gt;



&lt;p&gt;It started with the Covid-19 pandemic outbreak. Everyone wanted to pass their quarantine by reading books. But no one could go to the library due to the health restrictions. They had to keep social distance. &lt;/p&gt;

&lt;p&gt;Then Sarah came up with the idea to make an online deliverance system. Anyone could use the library's website to reserve books.&lt;/p&gt;

&lt;p&gt;She thought that she could deliver the books on her bike to their door front. And as the transportation cost her time and money, she took a very small amount of dollars for her service.&lt;br&gt;
But she wasn't always online and couldn't plan every books' deliverance on time. She had to deliver them herself and close the library since no one took her place.&lt;/p&gt;

&lt;p&gt;She needed extra help. Somehow.&lt;/p&gt;


&lt;h2&gt;
  
  
  🏇 In a hurry?&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;I have separated everything into different parts. Each of them is about a specific side of the &lt;code&gt;Promises&lt;/code&gt;. So you can skip any part you are confident about. &lt;/p&gt;

&lt;p&gt;Already know how to make &lt;code&gt;JavaScript Promises&lt;/code&gt;? Skip the introduction part!&lt;/p&gt;



&lt;p&gt;She called her cousin and asked him if he could design an ordering system for the library's website. She described the process as: "People will have to log in to their accounts. And if there is a book they want to request, they should put an online order ticket for it."&lt;/p&gt;

&lt;p&gt;But since it was not clear when she could deliver the books, the users had to wait for the website to notify them.&lt;br&gt;
It sent a message. If the book was available to deliver, it informed them she had &lt;strong&gt;resolved&lt;/strong&gt; the ticket. Otherwise, it contained a &lt;strong&gt;rejection&lt;/strong&gt; message.&lt;/p&gt;

&lt;p&gt;She named it the &lt;strong&gt;Promise System&lt;/strong&gt;. Likewise the &lt;code&gt;JavaScript API&lt;/code&gt; we're going to learn about.&lt;/p&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;


&lt;h2&gt;
  
  
  🤝 How Promises Are Made: Please Put A Ticket!&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;To create a &lt;code&gt;JavaScript Promise&lt;/code&gt; you can use the &lt;code&gt;new&lt;/code&gt; keyword:&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;thePromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Promise Object Constructor&lt;/code&gt; accepts a &lt;code&gt;function&lt;/code&gt; as an argument. This function runs the process that we're going to promise its fulfillment. &lt;/p&gt;

&lt;p&gt;In our story, the way Sarah manages the ordering through tickets resembles such a &lt;code&gt;Promise&lt;/code&gt;. Either it is &lt;code&gt;fulfilled&lt;/code&gt; with the book's delivery or &lt;code&gt;rejected&lt;/code&gt; for a reason. &lt;/p&gt;

&lt;p&gt;This function can in turn accept two arguments: &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt;.&lt;br&gt;
Both of which are callback functions that we can delegate on certain points. &lt;/p&gt;

&lt;p&gt;We use &lt;code&gt;resolve&lt;/code&gt; as a fulfillment signal and pass it the final data. By calling &lt;code&gt;reject&lt;/code&gt; we make clear that the &lt;code&gt;Promise&lt;/code&gt; has failed.&lt;/p&gt;

&lt;p&gt;From what we've learned we can construct a &lt;code&gt;Promise&lt;/code&gt; by writing the following lines:&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;theOrderingProcess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="c1"&gt;// The process with the unknown duration.&lt;/span&gt;
  &lt;span class="c1"&gt;// Function body goes here.&lt;/span&gt;

  &lt;span class="c1"&gt;// If anywhere in your code, &lt;/span&gt;
  &lt;span class="c1"&gt;// you think your promise is fulfilled, &lt;/span&gt;
  &lt;span class="c1"&gt;// (e.g. Sarah finds an ordered book is available),&lt;/span&gt;
  &lt;span class="c1"&gt;// you can call resolve and pass it &lt;/span&gt;
  &lt;span class="c1"&gt;// the data you would like to return. &lt;/span&gt;
  &lt;span class="c1"&gt;// Like the following:&lt;/span&gt;
  &lt;span class="c1"&gt;// resolve(theBookData)&lt;/span&gt;

  &lt;span class="c1"&gt;// But if somehow the promise has failed to fulfill, &lt;/span&gt;
  &lt;span class="c1"&gt;// (e.g. A book is currently borrowed),&lt;/span&gt;
  &lt;span class="c1"&gt;// you can call reject and&lt;/span&gt;
  &lt;span class="c1"&gt;// pass it the reason to rejection:&lt;/span&gt;
  &lt;span class="c1"&gt;// reject(theRejectionReason)&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;theWebsitePromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;theOrdeingProcess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Every time someone put a ticket for a book, he/she had to wait for a certain process to come to an end.&lt;br&gt;
It was not as if you selected a book and immediately had it on way to your home. Things took time. How long? No one knew for granted.&lt;/p&gt;

&lt;p&gt;Mrs. Perkins wasn't an all-up-to-date lady. So she had stored most of the data about the existing books and the borrowed ones in an old Access database. It took time to transfer them to a server. And Sarah wanted things to get done as soon as possible.&lt;/p&gt;

&lt;p&gt;She had to use the Access database. By herself.&lt;/p&gt;


&lt;h2&gt;
  
  
  🔛 Main Usages Of A Promise: Making API Calls.&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Nowadays most websites use a database on their back end side. Like how Mrs. Perkins stored the library's data in an Access database. &lt;/p&gt;

&lt;p&gt;Of course the website databases use automations!&lt;/p&gt;

&lt;p&gt;A web application's front end needs to send a request to a server endpoint to receive some data. Then using this data the application can deliver its services.&lt;/p&gt;

&lt;p&gt;As the front end sends the &lt;code&gt;request&lt;/code&gt;, the process mounts into a pending status. It will stay in this status until it either receives a response from the back end or receives none.&lt;/p&gt;

&lt;p&gt;The pending status could take for an unknown period. This depends on the networking conditions of the user and the server, and how fast they can transfer data. Additionally, the back end itself might need to process some data and take extra time.&lt;/p&gt;

&lt;p&gt;An example of sending a &lt;code&gt;request&lt;/code&gt; using the &lt;code&gt;JavaScript Fetch API&lt;/code&gt; looks like the following:&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;theRequest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://example.com/endpoint&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fetch method constructs a &lt;code&gt;Promise Object&lt;/code&gt;, and returns it. So we can use its returned value same as a simple &lt;code&gt;Promise Object&lt;/code&gt;. Helping us to get rid of creating our own &lt;code&gt;Promisess&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Want more comfort? Use axios.&lt;/p&gt;




&lt;p&gt;The library's online ordering system took tickets from its users. They contained information about the user and the selected book. Sarah reviewed the tickets. And checked for the books' availability in the Access database.&lt;/p&gt;

&lt;p&gt;If everything was okay, a green button labeled as &lt;code&gt;resolve&lt;/code&gt;, awaited Sarah's click. Otherwise, the big red button was ready to delegate a &lt;code&gt;rejection event&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;No one knows for sure. But rumors are there was a black button too. For dark purposes. Unknown.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✅❌ Callbacks: The Green And The Red Button.&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We call the processes like the API requests &lt;code&gt;Asynchronous Operations&lt;/code&gt;. They vary from the &lt;code&gt;Synchronous&lt;/code&gt; ones which are simple &lt;code&gt;Program Expressions&lt;/code&gt;. &lt;br&gt;
A &lt;code&gt;Synchronous Operation&lt;/code&gt; takes a constant time to complete. The system runs them in a sequence. It waits until the execution of one is complete before it runs the next.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Synchronous Operation&lt;/code&gt; looks like the following:&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;num2&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every line of this operation is predictable. The compiler will be executing each line one after another. There's nothing unpredictable that could block the program's main thread from running.&lt;/p&gt;

&lt;p&gt;But an &lt;code&gt;Asynchronous Operation&lt;/code&gt; can block the program's execution. This is so because its fulfillment depends on the network conditions and speed. Even if the user owned a fast network, the back-end servers could be facing trouble. This can result in no response or a longer-running process.&lt;/p&gt;

&lt;p&gt;How can we write an &lt;code&gt;Asynchronous Operation&lt;/code&gt; and make sure that it doesn't block the rest of the code from running?&lt;/p&gt;

&lt;p&gt;The answer is: "by using &lt;code&gt;Callbacks&lt;/code&gt;".&lt;/p&gt;

&lt;p&gt;Of course, while it sounds like a heartwarming name, wait until you are caught in a &lt;code&gt;callback hell&lt;/code&gt;!&lt;/p&gt;




&lt;p&gt;When the user put a ticket for a book, he registered for future events. He was awaiting a &lt;code&gt;response&lt;/code&gt; to his &lt;code&gt;request&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Either this response arrived, or some problem prevented it from ever coming.&lt;/p&gt;

&lt;p&gt;Now the user was using a true &lt;code&gt;Promise System&lt;/code&gt;. The library's website provided this system. But the user didn't have direct access to the promise. He was listening to it.&lt;/p&gt;

&lt;p&gt;Then the user had to make his own plans based on this &lt;code&gt;Promise&lt;/code&gt;. He was telling himself: if the reply was a successful message &lt;code&gt;then&lt;/code&gt; I'm gonna return my last book. If not, &lt;code&gt;then&lt;/code&gt; I can choose another one. Maybe the reply never came, or the website's server went down. This problem will &lt;code&gt;catch&lt;/code&gt; his eyes, and so he was going to call the librarian and inform her. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.warn('I never got a response!')&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  ↩️↪️ Callbacks: Then &amp;amp; Catch.&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;If you use the &lt;code&gt;JavaScript Fetch API&lt;/code&gt;, it will return a &lt;code&gt;Promise Object&lt;/code&gt; by default. You don't have to write the &lt;code&gt;Promise&lt;/code&gt; yourself. You have to listen to it. How?&lt;/p&gt;

&lt;p&gt;Every &lt;code&gt;Promise&lt;/code&gt; returns an &lt;code&gt;Object&lt;/code&gt;. And this &lt;code&gt;Object&lt;/code&gt; owns 2 important methods. &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;catch&lt;/code&gt;. You can use these methods to attach &lt;code&gt;callbacks&lt;/code&gt; to a &lt;code&gt;Promise&lt;/code&gt;. You pass them a &lt;code&gt;function&lt;/code&gt; which will be called as soon as the &lt;code&gt;Promise&lt;/code&gt; delegates an &lt;code&gt;event&lt;/code&gt;. What events?&lt;/p&gt;

&lt;p&gt;You can call the &lt;code&gt;resolve&lt;/code&gt; callback inside a &lt;code&gt;Promise Function&lt;/code&gt;'s body and pass it what data you want. Then the &lt;code&gt;Promise&lt;/code&gt; calls the &lt;code&gt;callback function&lt;/code&gt; inside the first &lt;code&gt;then&lt;/code&gt; call and passes the data to it. The &lt;code&gt;Promise&lt;/code&gt; calls the &lt;code&gt;catch&lt;/code&gt; method as soon as you call the &lt;code&gt;rejection&lt;/code&gt; callback.&lt;/p&gt;

&lt;p&gt;Let's visualize this process by an 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;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://example.com/endpoint&lt;/span&gt;&lt;span class="dl"&gt;"&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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&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;error&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="nx"&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;Error in fetching the request&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we call the &lt;code&gt;fetch function&lt;/code&gt; and pass it the &lt;code&gt;endpoint URL&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;fetch function&lt;/code&gt; creates a &lt;code&gt;Promise Object&lt;/code&gt; and returns it.&lt;/li&gt;
&lt;li&gt;We store this &lt;code&gt;Object&lt;/code&gt; in the &lt;code&gt;request&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;We call &lt;code&gt;request&lt;/code&gt;'s &lt;code&gt;then&lt;/code&gt; method and pass it a &lt;code&gt;function&lt;/code&gt;. The function can receive as many arguments as it expects. It retrieves those arguments from what the &lt;code&gt;Promise&lt;/code&gt; passes to the &lt;code&gt;resolve&lt;/code&gt; callback. Then the given function can do as much as it desires with the given data.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, we call &lt;code&gt;request&lt;/code&gt;'s &lt;code&gt;catch&lt;/code&gt; method. We pass it a function. And expect the &lt;code&gt;Promise&lt;/code&gt; to call this function when it &lt;code&gt;rejects&lt;/code&gt; or the &lt;code&gt;then&lt;/code&gt; method &lt;code&gt;throws&lt;/code&gt; an &lt;code&gt;Error&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the end we run the code to see if it works. Of course it does. And if not, we won't hesitate to point our finger at the back end:)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A question exists. The &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;catch&lt;/code&gt; methods only add callbacks to the &lt;code&gt;Promise Object&lt;/code&gt;. So why aren't they &lt;code&gt;properties&lt;/code&gt; to which we can attach the &lt;code&gt;callback functions&lt;/code&gt;? Wouldn't it be simpler?&lt;/p&gt;

&lt;p&gt;We'll find the answer.&lt;/p&gt;




&lt;p&gt;Everything Sarah did resulted in a &lt;code&gt;Promise&lt;/code&gt;. &lt;br&gt;
Hitting the &lt;code&gt;resolve&lt;/code&gt; button was only the first step. &lt;/p&gt;

&lt;p&gt;She had to find the book in the library. She had to package it next to the other ones which she had to deliver the following day.&lt;/p&gt;

&lt;p&gt;She had to mark each package with the right user's information. This included their names and addresses. She had to be careful when she loaded the packages on the bike. Not to disturb her ride.&lt;/p&gt;

&lt;p&gt;She had to ride through streets and alleys. Dodging traffics and caring for her safety. Reaching her destination, she had to ring the doorbell. If the client was present, &lt;code&gt;then&lt;/code&gt; she delivered the book.&lt;br&gt;
And &lt;code&gt;then&lt;/code&gt; she took the reader's previous book to return it to the library. &lt;/p&gt;

&lt;p&gt;After repeating the same routine for every client, she had to &lt;code&gt;finally&lt;/code&gt; go back to the library. She placed the returned books on the shelves. &lt;code&gt;Then&lt;/code&gt; filled the Access database with data about the returned and the delivered ones.&lt;/p&gt;

&lt;p&gt;After all, it was time to review that day's orders and check for their availability. Then managing to hit the &lt;code&gt;resolve&lt;/code&gt; or the &lt;code&gt;reject&lt;/code&gt; buttons.&lt;/p&gt;

&lt;p&gt;Even the rest she got by her night sleeps, was a promise she had made to her body.&lt;/p&gt;

&lt;p&gt;And it feels good to fulfill promises. Doesn't it?😊&lt;/p&gt;


&lt;h2&gt;
  
  
  ⛓️ The Methodology Behind Promises: Returning Objects!&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We've faced a question. A matter which insists that providing &lt;code&gt;callbacks&lt;/code&gt; through methods is useless. The &lt;code&gt;Promise API&lt;/code&gt; can instead provide special &lt;code&gt;Object Properties&lt;/code&gt;. Or the ability to add an array of &lt;code&gt;callbacks&lt;/code&gt;, for another instance.&lt;/p&gt;

&lt;p&gt;But think of all the possibilities you might face while developing front-end applications. You won't always want the &lt;code&gt;callbacks&lt;/code&gt; to run in order. You might not remember a case for this at the moment, but sure someday you'll face it. &lt;/p&gt;

&lt;p&gt;Different scenarios need different approaches. And if the &lt;code&gt;Promise&lt;/code&gt; is going to be of any help during these situations, it has to be flexible.&lt;/p&gt;

&lt;p&gt;Let's have a look at a piece of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;aPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;resolved&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;aPromise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have been able to use the &lt;code&gt;Promise&lt;/code&gt;'s static &lt;code&gt;resolve&lt;/code&gt; method. Without even having to construct a new &lt;code&gt;Promise Instance&lt;/code&gt; we were able to return a &lt;code&gt;Promise Object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we can call this &lt;code&gt;Object&lt;/code&gt;'s &lt;code&gt;then&lt;/code&gt; method. Or its &lt;code&gt;catch&lt;/code&gt; one. &lt;/p&gt;

&lt;p&gt;Well, what good is that for? You may ask. To find the answer let's have a look at another 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;firstPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;secondPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstPromise&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;boolean&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;boolean&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="nx"&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;JSON string was created successfuly!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JSON creation error&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="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;thirdPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstPromise&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; world!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The data doesn't contain a message!&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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newData&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&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;error&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error in third Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we've initiated a &lt;code&gt;Promise Object&lt;/code&gt; using only &lt;code&gt;Promise.resolve&lt;/code&gt;. This promise object gives us all the superpowers we want. We can call its &lt;code&gt;then&lt;/code&gt; method as much as we want, in a &lt;code&gt;chain&lt;/code&gt; or in &lt;code&gt;separate calls&lt;/code&gt;. This allows us to create various &lt;code&gt;waiters&lt;/code&gt; for the same process. What is a &lt;code&gt;waiter&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Let's say you have got to fetch data, which you will use in different parts of your application. But there's a problem. You can reuse &lt;code&gt;synchronous operation&lt;/code&gt; data as in &lt;code&gt;variables&lt;/code&gt;. But you can't do that with &lt;code&gt;asynchronous&lt;/code&gt; data. You should wait for it to become available.&lt;/p&gt;

&lt;p&gt;Now, there are different parts in your project, awaiting this data. We can refer to these parts as &lt;code&gt;waiters&lt;/code&gt;. They're observing the &lt;code&gt;Promise&lt;/code&gt;'s status and as soon it &lt;code&gt;settles&lt;/code&gt;, they will read its data.&lt;/p&gt;

&lt;p&gt;The above example showcases how the &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;catch&lt;/code&gt; methods help you to reuse the same data in different scenarios.&lt;/p&gt;

&lt;p&gt;This way the &lt;code&gt;Promise API&lt;/code&gt; is providing an easy-to-use solution.&lt;/p&gt;

&lt;p&gt;But it also provides us with some useful static methods. We can use them to handle different situations. They include: &lt;code&gt;Promise.all&lt;/code&gt;, &lt;code&gt;Promise.allSettled&lt;/code&gt;, &lt;code&gt;Promise.any&lt;/code&gt;, and &lt;code&gt;Promise.race&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;Every day, to return to the library Sarah had to make sure that she had visited every one of her destinations. If all the clients were present at home, the book exchanges were successful. Otherwise, some of them failed.&lt;/p&gt;

&lt;p&gt;She didn't need every deliverance to be &lt;code&gt;resolved&lt;/code&gt;. She needed to have finished the process which was about going to the client's house.&lt;/p&gt;

&lt;p&gt;In the end, she returned to the library. And when she was ready to enter the reports in the Access database, she would ask herself: "Was everyone at home? Did any of the orders remain undelivered?"&lt;/p&gt;

&lt;p&gt;She classified the undelivered books in a separate database. And she sent the clients an Email that expressed the situation.&lt;/p&gt;

&lt;p&gt;If she had delivered every book then, she only had to set that day's deliveries as done, in the database. Nothing further to take care of.&lt;/p&gt;




&lt;h2&gt;
  
  
  🕸️ Static Methods: &lt;code&gt;Promise.all&lt;/code&gt; &amp;amp; &lt;code&gt;Promise.allSettled&lt;/code&gt;&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;all&lt;/code&gt; method accepts an array of &lt;code&gt;promises&lt;/code&gt;. Then it waits for all the promises to &lt;code&gt;resolve&lt;/code&gt;. If any of the promises &lt;code&gt;rejects&lt;/code&gt;, it will immediately return with the rejection reason.&lt;/p&gt;

&lt;p&gt;This function behaves like what Sarah asks herself every night: 'Was everyone at home? Did any of the orders remain undelivered?' &lt;/p&gt;

&lt;p&gt;She will know that she has done all her tasks once she delivered the last book. If so, only the clients' returned books would be on her bike. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Promise.all&lt;/code&gt; resolves once every &lt;code&gt;Promise&lt;/code&gt; in the promises array passed to it resolves. It returns an array of what each promise had returned. Like the books that the library's clients returned.&lt;/p&gt;

&lt;p&gt;Sarah immediately knew that the delivery has failed If none of the clients were present at home. She would return with the undelivered book still on her bike. &lt;/p&gt;

&lt;p&gt;If any of the promises you pass to &lt;code&gt;Promise.all&lt;/code&gt; rejects, it will immediately reject. With the &lt;code&gt;rejection&lt;/code&gt; reason of the rejected promise.&lt;/p&gt;

&lt;p&gt;An example of &lt;code&gt;Promise.all&lt;/code&gt; usage:&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;client1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;returnedBooks&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;returnedBooks&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;rejectionReason&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="nx"&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;Error: a book was not delivered.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rejectionReason&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We said that Sarah's return to the library didn't depend on every client's presence. She needed to make the visit. So if she had toured around all the clients' addresses, she was ready to return to the library. And on her bike, she transported all the books. Whether returned or undelivered. &lt;/p&gt;

&lt;p&gt;For a likewise behavior with &lt;code&gt;JavaScript Promises&lt;/code&gt;, we can use &lt;code&gt;Promise.allSettled&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Every &lt;code&gt;Promise&lt;/code&gt; passes through two main states: &lt;code&gt;pending&lt;/code&gt; and &lt;code&gt;settled&lt;/code&gt;. The settled state is when the promise has been &lt;code&gt;fulfilled&lt;/code&gt;. The settlement either happens with resolve or a rejection. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;all&lt;/code&gt; method immediately returned when a &lt;code&gt;Promise&lt;/code&gt; rejected or every promise resolved. &lt;br&gt;
But the &lt;code&gt;allSettled&lt;/code&gt; method immediately returns. When all promises have either &lt;code&gt;resolved&lt;/code&gt; or &lt;code&gt;rejected&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When they are &lt;code&gt;settled&lt;/code&gt; indeed.&lt;/p&gt;

&lt;p&gt;What the &lt;code&gt;allSettled&lt;/code&gt; method returns in its &lt;code&gt;resolve&lt;/code&gt;, consists of an array of objects. Each object has a &lt;code&gt;status&lt;/code&gt; property which is either &lt;code&gt;fulfilled&lt;/code&gt; or &lt;code&gt;rejected&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If the status is &lt;code&gt;fulfilled&lt;/code&gt; then the object provides a &lt;code&gt;value&lt;/code&gt; property too. Otherwise, it owns a &lt;code&gt;reason&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;A demonstration of it in code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;allSettled&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;client1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;client3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
    &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fulfilled&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="nx"&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;A returned book&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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="nx"&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;An undelivered book&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reason&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;Soon enough troubles showed up.&lt;/p&gt;

&lt;p&gt;The website's users had to wait a long time for the response message's arrival. Then they started to see more rejection messages. No matter what book they ordered, a rejection message awaited them in their inboxes after 1 to 2 days.&lt;/p&gt;

&lt;p&gt;Some of the users tried to contact Sarah through Emails. But they only got a simple reply.  It stated: "The requested book is currently borrowed by someone else. You can try to order it later, or borrow another one."&lt;/p&gt;

&lt;p&gt;These replies worsened the situation. It surprised the clients that Sarah didn't even state when the book will be available. Or whether she could put them in a queue. &lt;/p&gt;

&lt;p&gt;Everything seemed unexpected and random.&lt;/p&gt;

&lt;p&gt;Sarah at first didn't notice any issue. She was still taking books for some people. But later it caught her eyes that things were getting weird.&lt;/p&gt;

&lt;p&gt;Before anything started to happen she wanted to find a solution to speed her routine up. She called her cousin and asked him if he could help her in searching the current Access databases. She needed to be quicker in finding out if a book was available.&lt;/p&gt;

&lt;p&gt;He said he would look into it and call her as soon as he found anything. The next day he had a solution. He could write a &lt;code&gt;python bot&lt;/code&gt; which would search every Access database. And to speed things up, he had found a useful algorithm for it too.&lt;/p&gt;

&lt;p&gt;He made the bot ready within a day. It searched the Access files for a specified book name and stated whether it was available to borrow. His special algorithm indeed created several &lt;code&gt;asynchronous operations&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;It searched each file in real-time with the others. As soon as any of the files contained a search result matching the book, the bot took it. Then it terminated all the search operations.&lt;/p&gt;

&lt;p&gt;She had separated the Access files into two different directories. She named one of them the &lt;strong&gt;library books&lt;/strong&gt; and the other the &lt;strong&gt;ordered books&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Books in the ordered books' directory were currently ordered or delivered. &lt;/p&gt;

&lt;p&gt;The library books databases had a list of all books in the library. It also contained information on whether a book was available or not.&lt;/p&gt;

&lt;p&gt;While developing his bot he had decided that the result was either in the &lt;code&gt;ordered books&lt;/code&gt; databases or the &lt;code&gt;library&lt;/code&gt; ones. If it was the ordered ones then it was obvious that the book wasn't available and Sarah had to reject the ticket. &lt;br&gt;
Else if it found the result in the library databases it was either available or not. &lt;/p&gt;

&lt;p&gt;This speed the search up because the ordered databases were smaller than the other group. So if it found a match result among them it terminated the searching quicker.&lt;/p&gt;

&lt;p&gt;But two weeks after the new bot's implementation most of the orders had to be rejected. She had had a regular delivery count of 9 to 10 books per day. Now, this number had fallen to as low as 3.&lt;/p&gt;

&lt;p&gt;Some days none at all.&lt;/p&gt;

&lt;p&gt;Many of the borrowed books remained unreturned for a long time. Their owners didn't even order new ones.&lt;/p&gt;

&lt;p&gt;And then she heard rumors. One of her best friends had tested positive for Covid-19. The infection statistics were surging in her town. She called some of her customers to ask why they weren't trying to return their books.&lt;/p&gt;

&lt;p&gt;Her suspicions proved right. Covid-19 had hit many of her customers. But some other ones were actually bothered with the site's functionality. They hated waiting a long time for no results.&lt;/p&gt;

&lt;p&gt;She tried to talk them back into using the library's website. She even promised that she will fix the issues. But &lt;strong&gt;promises&lt;/strong&gt; weren't convincing anymore. &lt;/p&gt;

&lt;p&gt;One night she called her cousin. Tears in her eyes. "I failed.", she said. The confession feeling bitter. "I failed badly."&lt;/p&gt;


&lt;h2&gt;
  
  
  🏃🏻‍♂️ Static Methods: &lt;code&gt;Promise.any&lt;/code&gt; &amp;amp; &lt;code&gt;Promise.race&lt;/code&gt;.&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Promise.any&lt;/code&gt; accepts an array of promises. And resolves as soon as &lt;strong&gt;any&lt;/strong&gt; of them &lt;code&gt;fulfills&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The library's users chose a book and ordered it and waited to see if it was available. If not, they would go for the next one. But this process took a long time. They had to wait for each order's message to arrive before they could decide anything.&lt;/p&gt;

&lt;p&gt;And when the system became unresponsive it wasted the users' precious time by days and weeks.&lt;/p&gt;

&lt;p&gt;What if they could find a way to order many books together and get the first book that was available? Of course, it was impossible with the library website's architecture. Sarah wouldn't have allowed this. This could ruin all her plans.&lt;/p&gt;

&lt;p&gt;But this is not the case with JavaScript. If you are waiting for the first promise that resolves, use &lt;code&gt;Promise.any&lt;/code&gt;. It works the same way as the hack the library users wished for.&lt;/p&gt;

&lt;p&gt;An implementation of &lt;code&gt;Promise.any&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;order1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;order2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;order3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&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="nx"&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;this order resolved first&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;order&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;error&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// expected output: "AggregateError: No Promise in Promise.any was resolved"&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As soon as any of the promises resolves, the &lt;code&gt;Promise Object&lt;/code&gt; calls the callback passed to &lt;code&gt;then&lt;/code&gt;. If all reject, it enters the &lt;code&gt;catch&lt;/code&gt; territory.&lt;/p&gt;

&lt;p&gt;Then there is &lt;code&gt;Promise.race&lt;/code&gt;. It resolves with the first &lt;code&gt;Promise&lt;/code&gt; that &lt;code&gt;settles&lt;/code&gt;. In case you've already forgotten, a &lt;code&gt;Settled Promise&lt;/code&gt; is either &lt;code&gt;fulfilled&lt;/code&gt; or &lt;code&gt;rejected&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, why should someone need to implement that? It's actually difficult to name a good use case for &lt;code&gt;Promise.race&lt;/code&gt;. But still, there are few times when it could be of help.&lt;/p&gt;

&lt;p&gt;Let's think about the bot that Sarah's cousin had developed. It exited the processes as soon as any of them returned anything. You might say that the bot's actual action is like what &lt;code&gt;Promise.any&lt;/code&gt; does. It returns as one of them &lt;code&gt;fulfills&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then it can be a bad example. But let's say that the &lt;code&gt;Promise&lt;/code&gt; which he used to search the &lt;code&gt;ordered books&lt;/code&gt; didn't fulfill when it matched a book. Let's say Sarah's cousin was smarter and played a trick on it.&lt;/p&gt;

&lt;p&gt;If the search in the ordered books matched a result then its &lt;code&gt;Promise&lt;/code&gt; rejected. Otherwise, if it had searched the entire databases and found none it resolved. But, if the search process related to the library books found a match and it was available then it resolved. Otherwise, the &lt;code&gt;Promise&lt;/code&gt; rejected.&lt;/p&gt;

&lt;p&gt;So here we have got a bright point. The rejection of one of the promises means the resolving of the other one. And vice versa.&lt;/p&gt;

&lt;p&gt;Now, &lt;code&gt;Promise.race&lt;/code&gt; can help us in reducing the time we need to wait for a useful response.&lt;/p&gt;

&lt;p&gt;Let's have a quick look:&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;race&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;searchLibraryBooks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;searchOrderedBooks&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;book&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="nx"&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;Book is available.&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="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;reason&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="nx"&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;Book is not available, because &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The library books have an alphabetical sorting. The ordered ones are sorted by their order date. So it's possible that the search in the whole library books could make a quicker match sometimes.&lt;/p&gt;




&lt;p&gt;It didn't take long before Sarah received a phone call from his cousin. He sounded anxious. And when she asked him what was wrong, he replied: "The bot had a bug, Sarah. There are always several versions of a book in the library. This means that if someone borrows one of its versions the book could still be available. I hadn't thought about this in my code. If one version of the book was ordered then I tagged it as unavailable. I'm sorry Sarah."&lt;/p&gt;

&lt;p&gt;Sarah was in shock for several seconds. How didn't this even cross her own mind?&lt;br&gt;
"Can you fix it?", she asked.&lt;br&gt;
"Well I'll do my best.", the other replied.&lt;br&gt;
"Then you better do it.", she couldn't control how loud her voice got. "And do it as soon as possible!"&lt;/p&gt;

&lt;p&gt;It took another day for them to fix the issue. She sent Emails to her clients. Informing that they have fixed the issue. And the users could start using the website immediately.&lt;/p&gt;


&lt;h2&gt;
  
  
  🔚 Promise Callbacks: &lt;code&gt;finally&lt;/code&gt;.&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We learned about &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;catch&lt;/code&gt;. But the &lt;code&gt;Promise Object&lt;/code&gt; also provides us with another method. It doesn't care if the &lt;code&gt;Promise&lt;/code&gt; fulfills or not. It only cares that it &lt;code&gt;settles&lt;/code&gt;. And any &lt;code&gt;then&lt;/code&gt; and &lt;code&gt;catch&lt;/code&gt; calls have already been delegated. Then it runs and does what you ask it for.&lt;/p&gt;

&lt;p&gt;An 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;websiteDelivery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;useServerDatabase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;websiteDelivery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useServerDatabase&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Use Access database&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Data isn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="nx"&gt;still&lt;/span&gt; &lt;span class="nx"&gt;moved&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;)
  )
  .catch(error =&amp;gt; console.log("Error in starting delivery website", error))
  .finally(_ =&amp;gt; console.log("No matter if you lost 100 times. Try for the 101st time!")
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Promise Object&lt;/code&gt; calls the function you passed to the &lt;code&gt;finally&lt;/code&gt; method in any condition. After the &lt;code&gt;Promise&lt;/code&gt; settles.&lt;/p&gt;




&lt;p&gt;"The devil is always in the details." This was what Mrs. Perkins told Sarah after she heard her story. She went on: "You saw the big picture. You wanted to start a movement and to modernize our town's library. But you forgot the details my dear. You told yourself that you can handle the database work. But this was something you had to leave to the programmer. After you've finally restored the library's daily life, I hope you've learned something. And I hope that this damned pandemic ends very soon."&lt;/p&gt;

&lt;p&gt;Sarah hoped too. And she smiled.&lt;/p&gt;




&lt;p&gt;It was a long read. And a longer one to write. I think it was worth the time.&lt;/p&gt;

&lt;p&gt;The hardest part was about &lt;code&gt;Promise.race&lt;/code&gt;. I could hardly find a good use case for it. And I hope the idea I came up with could help you make a picture of it.&lt;/p&gt;

&lt;p&gt;Do you think there are better use cases for any of these methods? Or do you think I have gotten something wrong? &lt;/p&gt;

&lt;p&gt;Please let me know in the comments. Writing this article helped me a lot in understanding the concepts myself. Your comments can help me and the others further too.&lt;/p&gt;

&lt;p&gt;Thanks for the reading.&lt;/p&gt;

&lt;p&gt;The cover image is by &lt;a href="http://www.epictop10.com/"&gt;Epic Top 10 Site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How technology and programming can benefit workplace safety.</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Sun, 13 Jun 2021 16:27:37 +0000</pubDate>
      <link>https://dev.to/alimobasheri/how-technology-and-programming-can-benefit-workplace-safety-24k4</link>
      <guid>https://dev.to/alimobasheri/how-technology-and-programming-can-benefit-workplace-safety-24k4</guid>
      <description>&lt;p&gt;&lt;em&gt;Cover image's source: &lt;a href="https://skycatch.com"&gt;skycatch&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every organization needs to take workplace safety seriously. Working in a safe environment results in improved work quality and is what every employee desires. &lt;br&gt;
Higher health and safety quality results in productivity improvements since healthy employees are more productive and will cause a positive impact on the company’s core process.  On the other hand, if employees feel insecure at work, productivity is minimized.&lt;/p&gt;

&lt;p&gt;After reading all of this, a question might occur to the reader: While every industry is quickly developing and technology is becoming the main subject in specialized work, how can we use it to improve the quality of workplace safety?&lt;/p&gt;

&lt;p&gt;In fact, what solutions does technology offer to extend the quality and usability of work safety?&lt;/p&gt;

&lt;h2&gt;
  
  
  Technology in the industrial context.
&lt;/h2&gt;

&lt;p&gt;Reading the above headline, probably the first thing you think of is the fully robotic process lines of the factories or the industrial design and assembly applications. Or you might think of the automated vehicles or Elon Musk's SpaceX rockets, pursuing the dream of inhabiting Mars. Yes! All of these are examples of the technology’s industrial usages.&lt;/p&gt;

&lt;p&gt;But have you ever wondered how a company ensures a worker’s safety among their factory’s giant machines? How could this benefit from technology? &lt;br&gt;
Basing several astronauts on a space station, how is their safety provided and secured? How is it that NASA measures Mars’ surrounding for its robotic probe from thousands of miles away and ensures its safety? Or how does the company utilize its drones to study the safety of the future of human life on Mars?&lt;/p&gt;

&lt;p&gt;Many questions exist. Industrial safety is a broad topic. And maybe someday we will need to take care of our robots’ safety in addition to that of the human. In this article, we will learn about some technology examples that can be helpful in the workplace safety field.&lt;/p&gt;

&lt;h3&gt;
  
  
  Realtime Connections
&lt;/h3&gt;

&lt;p&gt;High-speed communication and networking tools can have an essential effect in improving workplace safety. Too many risks surround the workers who need to work in remote environments.&lt;/p&gt;

&lt;p&gt;One of the technology examples I mentioned was about how NASA ensured its probe’s safety on Mars. To communicate with its probe on the surface of the red planet, the organization has set up an extensive communication chain called the Relay Network. &lt;br&gt;
It includes two satellites in orbit around Mars that receive commands from Earth and send them to the probe. Images and scientific data received from the probe will be sent back to Earth by the Relay Network(&lt;a href="https://b2n.ir/j79135"&gt;Learn more on NASA’s website&lt;/a&gt;).&lt;br&gt;
This way, NASA can receive the instantaneous conditions of the rover and safely steer and control it by sending scheduled commands.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CS8RtpSB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drlo0h00ywz6wahek54e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CS8RtpSB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/drlo0h00ywz6wahek54e.jpg" alt="A visualization NASA's Relay Network"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Currently, the Internet is the most widespread real-time communication tool on Earth, allowing people to connect regardless of their location. Industrial organizations can easily use this powerful tool and the communication platform it builds to communicate with their employees.&lt;/p&gt;

&lt;p&gt;Communication apps and tools can collect real-world data, enabling employers to be aware of their employees' safety status. Employers must be able to receive up-to-date information about the workplace’s safety and health status. They can achieve this by using connection technologies.&lt;/p&gt;

&lt;p&gt;Web applications can be an effective tool in this field. The web is a platform that can run on any host, enabling responsive and effective user interfaces.&lt;/p&gt;

&lt;p&gt;For example, NASA has announced they intend to launch their central control system, which is an open-source application for various platforms such as desktop and mobile. They have based it on web technologies using the VueJs web programming framework (&lt;a href="https://nasa.github.io/openmct/"&gt;More information&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;This shows how the web and its programming languages’ flexibility can facilitate the development of communication tools.&lt;/p&gt;

&lt;h3&gt;
  
  
  Safety Applications
&lt;/h3&gt;

&lt;p&gt;The intention behind the development of safety apps is to simplify the access to information needed about the risks involved in a process. &lt;/p&gt;

&lt;p&gt;These apps receive the information related to your projects, create a database of it, and by examining various cases, allow you to predict possible risks or accidents. &lt;br&gt;
They organize safety checklists, store photos of work phases, measure the project’s progress, and convey the state of the risky steps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q49U8T9F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yj0xph163crg7kt98xgo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q49U8T9F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yj0xph163crg7kt98xgo.png" alt="Screenshot of SurveyMonkey Application: An online form creation tool."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These tools are very helpful in managing occupational safety and health and facilitate the process of taking care of operations.&lt;br&gt;
Mobile apps development has become much simple than in the past, and its users are expanding rapidly. &lt;/p&gt;

&lt;p&gt;And in the development community, new technologies and solutions have been introduced that can help to develop a comprehensive application suitable for several target platforms using one programming language and developing under one platform.&lt;/p&gt;

&lt;p&gt;This is exactly why an organization like NASA uses a web framework for its central control system application.&lt;/p&gt;

&lt;h3&gt;
  
  
  3D Visualization
&lt;/h3&gt;

&lt;p&gt;Three-dimensional modeling can help employees in a work environment to learn more about the workplace and to be aware of the possible dangers.&lt;/p&gt;

&lt;p&gt;For example, if the SpaceX company decides to test its space rockets, a visualization of the launch environment can demonstrate the rocket launch process to personnel. Then they can obtain a safe radius by examining the likelihood of a problem or an explosion and use this visualization to prepare the test environment to prevent any casualties or side effects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mGTzvTIA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zjblac4hp5c59d36x9cu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mGTzvTIA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zjblac4hp5c59d36x9cu.png" alt="NASA's HL-20 simulation with Simulink."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Employers can also use this technology to train employee safety. In three dimensions in a reality-like environment, It is possible to convey the steps required to get the job done securely and identify the hazards and accidents that are likely to occur.&lt;/p&gt;

&lt;p&gt;In other cases, we can use simulation to design an industrial process and pre-test safety in a specific environment. We can also benefit from it to maintain inherently safe workplaces and chemical processes, which depend on the structure and the building of the workplace ( to design an inherently safe process, we should get rid of every predictable risk factor in its prototype stage).&lt;/p&gt;

&lt;p&gt;Designers have been using 3D design software for a long time. But what has recently made 3D models more widely used than before is their use in the context of the web. This way, advanced modeling can be performed and reviewed in any environment without the need for advanced devices.&lt;/p&gt;

&lt;p&gt;As in the case of a worker who wants to review a particular operating method in his work environment and remember how to do it safely, watching such models through smartphones can accelerate his work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Augmented Reality and Virtual Reality
&lt;/h3&gt;

&lt;p&gt;Safety training and learning is the first fundamental step in developing a safety culture, but as the saying goes, experience is the best teacher. &lt;br&gt;
Yet, in the matter of safety, one cannot rely on experience alone. An unsafe experience can be too painful and lead to horrific events such as amputation or chronic illness.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--70L6rlzf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2usq0xz4tg9m9neyvfsj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--70L6rlzf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2usq0xz4tg9m9neyvfsj.png" alt="A man wearing VR headsets to use VR for hazard awareness."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Virtual reality can help people observe and experience various situations, including dangerous events, and acknowledge appropriate reactions in a 100% safe environment by placing them in a virtual simulation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Artificial Intelligence and Machine Learning
&lt;/h3&gt;

&lt;p&gt;We may argue that artificial intelligence and machine learning are not directly acceptable as safety technologies. But, predictably, these topics of computer science will have a role in improving safety technologies.&lt;br&gt;
We use safety as a science to prevent the occurrence of risky probabilities. And when it comes to probability and statistics, computers come in handy.&lt;/p&gt;

&lt;p&gt;For example, one of the difficulties of safety in the workplace is monitoring all available situations. On the other hand, many accidents occur due to inattention or distraction of human operators.&lt;/p&gt;

&lt;p&gt;As a solution, we can use drones equipped with cameras that can monitor various processes in a workplace. Then by using computer image processing, artificial intelligence analysis, and machine learning, the system can check whether a potential risk or danger exists or not.&lt;br&gt;
In more detail, with the help of artificial intelligence, this drone can detect whether a worker is wearing gloves in a situation where it is essential to wear them or whether they have dangerously left work waste in the process’s route.&lt;/p&gt;

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

&lt;p&gt;We learned how modern technologies help facilitate the process of assessing and identifying the hazards in a workplace. &lt;br&gt;
The above is just a glimpse of examples of these technologies. In future articles, readers can expect a deeper and more comprehensive explanation of each of these technologies and the new ways of their utilization.&lt;br&gt;
At an applied level, these technologies can attract Industrial Safety and HSE activists. And at a basic level, most of them require programming, computer, and network knowledge. I will try to cover both of them in future articles.&lt;/p&gt;

&lt;p&gt;Artificial intelligence and machine learning subjects are among the important ones that lead every technology’s future. The world is moving towards automated and error-prone work routines, and certainly, in the near future, these subjects will change the way we implement Industrial Safety. That is why I will continue to write about them, as they are in my interests too.&lt;/p&gt;

&lt;p&gt;This article was originally published on the &lt;a href="https://hse-land.com/en/posts/How%20technology%20and%20computer%20science%20can%20benefit%20workplace%20safety"&gt;HSELand&lt;/a&gt; blog.&lt;/p&gt;

&lt;p&gt;And I appreciate Sandra Melo’s &lt;a href="https://mydatascope.com/blog/en/how-to-use-technology-to-improve-workplace-safety/"&gt;article&lt;/a&gt; on mydatascope which inspired me in writing this post.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>hse</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>👨‍🔬 Why The DOM Causes Negative Enthalpy! - Learning Frontend Deeply - Part 2</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Sat, 24 Apr 2021 20:23:11 +0000</pubDate>
      <link>https://dev.to/alimobasheri/why-the-dom-causes-negative-enthalpy-learning-frontend-deeply-part-2-1k8n</link>
      <guid>https://dev.to/alimobasheri/why-the-dom-causes-negative-enthalpy-learning-frontend-deeply-part-2-1k8n</guid>
      <description>&lt;h1&gt;
  
  
  Why Should You Care About The DOM And The Negative Enthalpy?
&lt;/h1&gt;

&lt;p&gt;In the first part of this series, we learned that &lt;code&gt;HTML&lt;/code&gt; is a &lt;code&gt;Markup Language&lt;/code&gt; destined to create documents. We now know that this document is much like a paper one. We can use different elements and layout systems in HTML to shape our ideal document layout.&lt;br&gt;
But still, there's a question. If it's only about documents, then humans have been able to create them thousands of years ago. Documents can be carved on stone, painted on papyruses, and written on paper.&lt;br&gt;
So while &lt;code&gt;HTTP&lt;/code&gt; facilitates the document's sharing around the world, there's one important thing that a webpage needs to be capable of to ultimately become superior to a simple document. &lt;/p&gt;

&lt;p&gt;And it is the ability to update data in real-time, in response to user interactions and different events. This feature makes documents interactive and pushes the limit of the traditional documents which could never be edited or updated. And especially this is one of the main skills every frontend developer should acquire.&lt;/p&gt;

&lt;p&gt;Throughout this article, we are going to learn about the &lt;code&gt;Document Object Model&lt;/code&gt; abbreviated as the &lt;code&gt;DOM&lt;/code&gt;. In the last part, we learned about the &lt;strong&gt;Document&lt;/strong&gt;, now we'll try to figure out what an &lt;strong&gt;Object&lt;/strong&gt; is and how the &lt;code&gt;DOM&lt;/code&gt; implements the &lt;strong&gt;Model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But we're not going down a straightforward path. We're going to learn about some basic theories of computer programming. This includes variables, objects, collections, compilers and etc. This is because I want to show you how in the end all of these theories shape the internal functionalities of a web frontend application.&lt;br&gt;
This is somewhat like how we're introduced to Christopher Nolan's Tenet. It's going to show you some theories and in the end, you are going to face it all in real action. Then let's dive deeper!&lt;/p&gt;

&lt;p&gt;First questions first...&lt;/p&gt;
&lt;h2&gt;
  
  
  🏍️ What Is An Object?
&lt;/h2&gt;

&lt;p&gt;While a motorcycle has two wheels, a car holds four. Both are &lt;strong&gt;objects&lt;/strong&gt;. Each with its traits. These traits could vary in numerous ways.&lt;/p&gt;

&lt;p&gt;The comparison we made identifies the difference in the count of common property in two distinct objects.&lt;br&gt;
A contrasting case is the observation of a hen and a motorcycle. Both can move. And while the hen utilizes its legs for this purpose, the other uses its wheels. The movement &lt;strong&gt;action&lt;/strong&gt; is possible for both of them, but they do so using quite different &lt;strong&gt;tools&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;❗ In terms of programming, we can express the actions as &lt;strong&gt;Methods&lt;/strong&gt; and the tools as &lt;strong&gt;Properties&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thus, the main element in Object Definition is that &lt;strong&gt;a set of traits shape the object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By knowing what an object definition is, let's move on to how a document is defined by it.&lt;/p&gt;
&lt;h2&gt;
  
  
  🌴 What is an object model?
&lt;/h2&gt;

&lt;p&gt;C is a programming language. So is C++. Both are known as semi-low-level languages. This means you'll need to write thousands of lines of code to make a simple program work. But in exchange, their programs run at higher speeds. Because by writing code in a low-level language the system needs less translation of your code to understand what you're trying to assemble.&lt;/p&gt;

&lt;p&gt;But there is a major difference between the two languages we talked about earlier. C++ is an object-oriented version of C. What does this mean?&lt;br&gt;
This means that we can define objects in C++ programs which possess their own traits and actions. &lt;/p&gt;

&lt;p&gt;Let's define object orientation in smaller chunks. That'll make it easier for you to grasp the idea if you don't already know about it. &lt;/p&gt;

&lt;p&gt;First, let's begin with a simple thing; Value. That's what every program is about!&lt;/p&gt;
&lt;h3&gt;
  
  
  2️⃣ Hey PC, Take This 2!
&lt;/h3&gt;

&lt;p&gt;In a program, every value is stored in a part of the memory. This value is identified by a reference. A reference is a specific number that addresses a location in the memory that holds a specific value.&lt;/p&gt;

&lt;p&gt;This referencing can be suitable for performing actions like computing. For example, if you want to calculate the sum of 2 and 3, you have to store these values in the system and then give the system the program by which it should add these two numbers. &lt;br&gt;
A reference to the value 2 could be a number like 2452123 and a reference to the value 3 could be another number like 7892392. &lt;/p&gt;

&lt;p&gt;The system can manage these references easily. But it would be hard for a human to work with them. We'll easily forget which reference points to which value. &lt;/p&gt;
&lt;h3&gt;
  
  
  🤙 Call My 2, Ey!
&lt;/h3&gt;

&lt;p&gt;A variable is simply a name we give the reference to a value. In the last paragraph, we said that in an exemplary system a reference to number 2 is 2452123. &lt;br&gt;
Now, what if we told the system that we want this reference to be called &lt;code&gt;a&lt;/code&gt;; so that every time we had to point to this number we simply give its name and the system retrieves the value for us?&lt;/p&gt;

&lt;p&gt;
  How variables resemble humans' memory. (Click to learn more.)
  &lt;p&gt;This behavior is close to how we humans store different data in our minds. For example, when we want to refer to a long fruit with a yellow cover, we say banana. The value was retrieved!&lt;/p&gt;

&lt;/p&gt;

&lt;h3&gt;
  
  
  🎙️ Hey PC, Repeat After Me: Ey equals 2!
&lt;/h3&gt;

&lt;p&gt;Fortunately, most of the programming languages manage this for us. &lt;br&gt;
Here's how we define a variable in JavaScript:&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;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code block, we've declared two values, stored them in memory, and given them a custom name for their references. Thus if we wanted to add these numbers we simply tell the system: &lt;code&gt;add a and b&lt;/code&gt;.&lt;br&gt;
In JavaScript it's written like this:&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;var&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;
  What happened in this code block?(click to learn more)
  &lt;p&gt;Here, in a single line, we've performed three actions. First, we've retrieved the two values 2 and 3 from the memory, by calling their names. Then, we've added these two numbers which result in a new value, 5. Next, the new value is stored in the memory and its reference is given a name; &lt;code&gt;c&lt;/code&gt;.&lt;/p&gt;

&lt;/p&gt;

&lt;p&gt;Well, we gave the references a name. But what does it have to do with Object-Orientation?&lt;/p&gt;

&lt;h3&gt;
  
  
  ⛏️ The Thing, or The Object?
&lt;/h3&gt;

&lt;p&gt;So far, we've only defined simple variables. These may be the basics of programming, but they are insufficient for a more advanced program.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Metal is one of the most useful materials in construction but by giving a fair look at the world around you, it's easy to conclude that buildings aren't just made of metals. They're assembled of glasses, metals, concrete, etc. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The same observance applies to a program. It's never made of single values. But rather a collection of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  👨‍👩‍👧‍👦 [Person 1, Person 2, Person 3]
&lt;/h3&gt;

&lt;p&gt;You know people by their names, phone numbers, looks, jobs, and much more. Perhaps you know a lot of people. Hundreds of names could be familiar to you. &lt;/p&gt;

&lt;p&gt;Indeed you have got a collection of information in your memory. A collection of names, or a collection of different brands. All in all, collections are the main way of information storage.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What makes collections suitable for this purpose is their flexibility and simplicity in integration. You can easily search, sort, filter, or manipulate a collection.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  📊 Data Structures
&lt;/h3&gt;

&lt;p&gt;Probably, algorithms are familiar to you. If not, you can think of the following text as an algorithm:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Go to the kitchen. Search the cabinets for the red pepper can. If found, swallow the whole can's content. If not, go to the freezer. Take out all the ices in it and swallow them one by one.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you can see, an algorithm is a set of step-to-step commands. Computer programs are instructed using these algorithms.&lt;/p&gt;

&lt;p&gt;Let's think about the sum program which we previously wrote using the variables. Its algorithm is straightforward. Take the first number and add it to the second one. Store the result in a new memory location.&lt;/p&gt;

&lt;p&gt;But is there a collection too?&lt;/p&gt;

&lt;h3&gt;
  
  
  🔢 Arrays
&lt;/h3&gt;

&lt;p&gt;Sometimes you need to store different values as a group. Like a list of different versions of a sentence. You can store each value in a separate variable but that's not the ideal way. Because for instance, if you are required to iterate over the list and find a specific version, you'll have to check each value manually to find out whether it's the wanted value or not.&lt;/p&gt;

&lt;p&gt;Arrays come to help.&lt;br&gt;
An array is indeed a list of memory addresses. Of course, these memory addresses refer to values. But its difference with a normal reference is that it can be indexed. &lt;/p&gt;

&lt;p&gt;
  What is indexing? And how does an array work? (click to learn more)
  &lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simply it's like when you are looking at a list of different versions of a sentence and you ask your colleague about which version he likes the most and he responds: 'The &lt;strong&gt;third&lt;/strong&gt; one!'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So, the keyword here is &lt;code&gt;third&lt;/code&gt;.&lt;br&gt;
Now if we wanted to represent the version history list in a JavaScript array, it would look like this:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;versions&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="s1"&gt;A sentence.&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;Beautifully written sentence.&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;Da Funny Sendenze!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;To access the second sentence and store it in a new variable the following line of JavaScript suffices:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;theDesiredSentence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;versions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;In JavaScript arrays are indexed from 0 which means that the first element's index is 0 and the nth element's index is &lt;code&gt;n-1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;/p&gt;
&lt;p&gt;The array is a very simple collection. But remember when we talked about the calculation program. It didn't consist of any arrays. Perhaps we could use an array of numbers and write a program to calculate the sum of all of the numbers in the array. But that's not what we're trying to do right now.&lt;/p&gt;

&lt;p&gt;The question was whether, in that simple program, a collection existed or not. Now, none of the values were collections, but in reality, the whole program is a collection.&lt;/p&gt;

&lt;p&gt;Why is that?🧐&lt;/p&gt;
&lt;h3&gt;
  
  
  🔁 Program Compilation
&lt;/h3&gt;

&lt;p&gt;Every program written in a language has to be compiled into machine language to act as fast as possible. The machine language is the most direct one, but having the lowest level among the programming languages, it's not possible for programmers to easily interact with it.&lt;br&gt;
The nerds' solution to this problem has been the development of higher-level programming languages. Yes, even C++ has a higher level in comparison to that of Assembly.&lt;/p&gt;

&lt;p&gt;Still, there's a matter to reflect upon. If communication with machines is difficult then how comes the compilers do it and turn huge chunks of code into machine-readable ones?&lt;/p&gt;

&lt;p&gt;To understand the mechanism you can think of yourself trying to speak a foreign language, how will you manage it? &lt;br&gt;
First, you will create mental models.&lt;/p&gt;

&lt;p&gt;What is the mental model? It's the concept or the meaning of what you are trying to translate.&lt;/p&gt;

&lt;p&gt;How is a mental model designed? Suppose you want to tell a foreigner that his face is burning. What is the concept behind this sentence? A face that belongs to that person is melting due to contact with heat? &lt;br&gt;
But what is the context? Perhaps you and the other person are trapped inside a burning house and you are screaming at him to warn him that his face is burning. Or maybe you are pushing his face into boiling water and joyously shouting: "Ha! Ha! Ha! Your face is burning!"&lt;/p&gt;

&lt;p&gt;Do you see? Different contexts. Different tones. Different sentence structures.&lt;/p&gt;

&lt;p&gt;Now how are these related to a compiler's task? Well first of all it gathers every value in your program. These values are like the meanings of every single word. Next, it tries to shape a model of your values. This model is shaped from the different scopes inside a program code. &lt;br&gt;
Scopes are different blocks of code inside a program. These blocks contain standalone logics, which can perform independently of other parts of the code. Sure most of the time the blocks will use variables defined in other scopes or passed as arguments.&lt;/p&gt;

&lt;p&gt;The compiler will look for the various blocks present in a code to shape its model. These blocks will help in maintaining the levels of the model. Previously we learned about arrays which are the most common forms of collections, but the ones we spoke about were only one-dimensional. However, in cases in which we need to specify a collection of groups of values, we can simply nest arrays inside each other.&lt;/p&gt;

&lt;p&gt;In JavaScript a nested array might look like this:&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;var&lt;/span&gt; &lt;span class="nx"&gt;nestedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&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 the above code example, the &lt;code&gt;nestedArray&lt;/code&gt; variable could be a model of various values organized in different blocks. Like &lt;code&gt;block 0&lt;/code&gt; and &lt;code&gt;block 1&lt;/code&gt; and so on. This way the compiler will know what block each value belongs to. So if somewhere in your code you try to call a value that isn't available in the corresponding block, the compiler will throw an error.&lt;/p&gt;

&lt;p&gt;A nested array might be a good example to illustrate a collection model, but not perfect for such a case as a compiler's model. Because arrays are just a group of values in a specific order.&lt;/p&gt;

&lt;p&gt;Thus, the programmers have designed various types of data structures that can be used to implement collections in a useful way. Examples of these data structures include linked lists, queues, stacks, graphs, and hash tables.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Data Structure Does A Compiler Use?
&lt;/h3&gt;

&lt;p&gt;Compilers mainly use &lt;code&gt;Symbol Tables&lt;/code&gt; as their primary data structure. &lt;br&gt;
A &lt;code&gt;Symbol Table&lt;/code&gt; is a symbolized collection of data. Don't panic if this doesn't make sense to you, we're going to learn it in more detail.&lt;/p&gt;
&lt;h4&gt;
  
  
  ⚛️ What does symbolized mean?
&lt;/h4&gt;

&lt;p&gt;Remember when we talked about variables? &lt;br&gt;
We stored a value in the memory and then gave it a name. So each variable in a program is made of a group of information that includes: &lt;code&gt;memory reference&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;type&lt;/code&gt;, and &lt;code&gt;attribute&lt;/code&gt;.&lt;br&gt;
Using these properties, the compiler can store what information it requires about a particular variable in a single symbol and then implement these symbols into a bigger model which represents the code blocks and scopes, using a data structure.&lt;/p&gt;

&lt;p&gt;A representation of &lt;code&gt;Symbol Tables&lt;/code&gt; could look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;symbol&lt;/span&gt; &lt;span class="err"&gt;name,&lt;/span&gt; &lt;span class="err"&gt;type,&lt;/span&gt; &lt;span class="err"&gt;attribute&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  
  What are the type and the attribute? (click to learn more)
  &lt;ul&gt;
&lt;li&gt;JavaScript is a dynamically typed language, which means that you don't have to strictly define a variable's type. But under the hood, each value owns a type. There are several built-in types like objects, strings, numbers, etc.&lt;/li&gt;
&lt;li&gt;These types declare the intrinsic behavior of the variables. So in a &lt;code&gt;Symbol Table&lt;/code&gt;, each value holds a type declaration.
The attribute is another unpopular term in JS. In languages like Java, keywords such as &lt;code&gt;public&lt;/code&gt; and &lt;code&gt;private&lt;/code&gt; exist that can be used in a variable declaration to indicate in what context the variable can be used. &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are the two attributes that can be used in JS. For instance, using the &lt;code&gt;const&lt;/code&gt; attribute clarifies for the compiler that the variable can't be assigned a new value after its initial declaration.&lt;/li&gt;
&lt;/ul&gt;




&lt;/h4&gt;
&lt;p&gt;What data structure a compiler uses for shaping the code blocks, can vary between &lt;code&gt;Linear Lists&lt;/code&gt;, &lt;code&gt;Binary Search Tree&lt;/code&gt;, and &lt;code&gt;Hash Tables&lt;/code&gt; based on the compiler's architect.&lt;/p&gt;
&lt;h4&gt;
  
  
  💼 Is a data structure enough for a compiler to get its job done?
&lt;/h4&gt;

&lt;p&gt;The short answer is no. &lt;br&gt;
The long answer is that a data structure is only a model that makes data available to you. It exposes no methods to work with the data. It's barely a skeleton of data. &lt;br&gt;
A skeleton doesn't move by itself. It can take no action. A body needs muscles to be able to handle its skeleton for good use.&lt;br&gt;
Hence the compiler uses its own built-in methods to work with the data that is exposed to it through the symbols.&lt;/p&gt;

&lt;p&gt;
  How is this data structure is similar to a database? (click to learn more)
  &lt;ul&gt;
&lt;li&gt;Each symbol is an &lt;code&gt;entry&lt;/code&gt; and each code block is called a &lt;code&gt;block&lt;/code&gt;. You could think of a symbol table as a database. Indeed I can relate to this personally since I'm currently working on the development of a web-based database management application for the place I work at.&lt;/li&gt;
&lt;li&gt;Each cell in a database table is like an &lt;code&gt;entry&lt;/code&gt;. A cell can be a text, a number, a date, and many more fields. Each of these fields has its own type and attributes.
Each table can also be divided into different phases with each phase having its own rows of entries.&lt;/li&gt;
&lt;li&gt;But a database management application isn't made up of just entries and phases. It's also about the ordering of data, its aggregation, editing, inserting, removing, validating and etc.&lt;/li&gt;
&lt;li&gt;Each of these functionalities can also be generalized to how a compiler commonly behaves with a symbol table.
&lt;/li&gt;
&lt;/ul&gt;



&lt;/p&gt;
&lt;h3&gt;
  
  
  ⁉️ What now?
&lt;/h3&gt;

&lt;p&gt;This was a long read, but not a useless one. We have learned about the program's compilation progress, and we've come to acknowledge what a data structure is and how an object is defined.&lt;br&gt;
Now, it's time to get back on track and learn how a Document's Object Model is implemented.&lt;/p&gt;

&lt;p&gt;But first, let's take a few seconds and think about the answer to a fundamental question of the frontend area.&lt;/p&gt;

&lt;h4&gt;
  
  
  🤷‍♂️ Is HTML a Data Structure or is it an Object?
&lt;/h4&gt;

&lt;p&gt;It's tempting to say that HTML is an &lt;code&gt;object&lt;/code&gt; since it possesses traits like a &lt;code&gt;body&lt;/code&gt; or a &lt;code&gt;head&lt;/code&gt; or a &lt;code&gt;title&lt;/code&gt;. These may sound like traits being defined for an object.&lt;br&gt;
But they're not and HTML is definitely not an &lt;code&gt;Object&lt;/code&gt;. It's a &lt;code&gt;Data Structure&lt;/code&gt;. All that HTML does is expose data. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Indeed you the developer who writes the HTML are exposing this data to the browser. Using the document structure which we talked about in the last part, you were doing the compiler's job. &lt;br&gt;
Oh, yes! Perhaps you have written several HTML documents, or maybe you're an experienced one, and using the HTML markup has been a daily job for you. But you have never thought that you were indeed writing a pre-compile file. That you were doing the programming process with a negative enthalpy!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sounds crazy and sounds like what Christopher Nolan introduced in his 2020 movie; Tenet.&lt;/p&gt;

&lt;p&gt;You have come a long way to reach this point, and perhaps you are tired or you think this article will be useless. Yeah, I know! This idea is turning around your head that I've been mocking you all the time. That compilers and data structures and the Object Orientation have got nothing to do with a simple HTML.&lt;br&gt;
But be patient my dear reader. From now things only get better. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PkTC8oAo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv5tsk8fdz7dc6vxh75w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PkTC8oAo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sv5tsk8fdz7dc6vxh75w.gif" alt="A fight scene in Tenet"&gt;&lt;/a&gt;&lt;br&gt;
After all, what do you expect from a Nolanish idea?😎&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SPOILER FREE&lt;/strong&gt;: I'm not going to spoil anything from the movie's plot. The negative enthalpy theory is something you learn very early in the movie. So enjoy the article!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  🔀 But how is the negative enthalpy happening?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A chemical reaction with a negative enthalpy is one that loses energy throughout the process.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This might sound too theoric but we can interpret it in our subject as such:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When we code a program and the compiler turns it into an executable file, we're keeping energy by doing less work by ourselves. The compiler is the one that handles the hassle of translating our code to machine-readable programs. Now, when we write HTML, we're losing energy. Because we're doing a part of the compiler's job which is creating a &lt;code&gt;data structure&lt;/code&gt;. We're doing more job to model the fine Document Structure for our purpose.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But the &lt;code&gt;Data Structure&lt;/code&gt; isn't the only thing that the compiler creates. There was actually a need for different methods to be able to work with this data. &lt;br&gt;
This is where the &lt;code&gt;Document Object Model&lt;/code&gt; comes into play. And to no surprise, this time we're going to work with &lt;code&gt;objects&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  🗜️ Where are my methods?
&lt;/h3&gt;

&lt;p&gt;So, you give the browser all the data necessary in your document, organized into nested structures, according to your desired layout, and expect the magic to happen. &lt;br&gt;
But that's not what every application is about. Is it? Nowadays web applications are more than just scientific documents. They're about &lt;code&gt;Interactive Interfaces&lt;/code&gt; which respond to user interaction. &lt;/p&gt;

&lt;p&gt;Documents get updated, changed, inspected, animated, styled, and manipulated in real-time. If you think of the &lt;code&gt;HTML Document&lt;/code&gt; as the &lt;code&gt;Symbol Table&lt;/code&gt; then by using the &lt;code&gt;DOM Methods&lt;/code&gt; to work with this data structure and to change it, you are doing the compiler's job. &lt;br&gt;
Because you are managing the available data structures and methods to output your desired program. &lt;/p&gt;

&lt;p&gt;But there is one important difference. The compiler compiles once, you do it many times.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;Originally this post was intended to have full coverage of all the methods that the &lt;code&gt;DOM&lt;/code&gt; avails us with. But tonight I decided that writing such a long article might not be a wise move and might result in the dear readers' attention loss.&lt;/p&gt;

&lt;p&gt;So our long journey comes to a pause with the knowledge that the management of a frontend application is like a real-time compilation. In the next part, we'll learn about the &lt;code&gt;DOM&lt;/code&gt;'s methods and compare each of them with what a compiler does.&lt;/p&gt;

&lt;p&gt;The next part is more like an action ride through how updating a document's different parts, results in different states, and perhaps Tenets!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MdWo2J7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zid3nf9xdcmn07tleib.GIF" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MdWo2J7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zid3nf9xdcmn07tleib.GIF" alt="Tenet gif. Pattinson: What the hell happened here? Washington: It hasn't happened yet."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And I expect the next part to be released faster than this one. Since most of it is already written!😁&lt;/p&gt;

&lt;h3&gt;
  
  
  The End!
&lt;/h3&gt;

&lt;p&gt;And don't forget to give me feedback. I'm trying to bring new ideas into this series and like everyone else my ideas have their own flaws too. So I would be glad to hear what you think!😅&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>html</category>
      <category>frontend</category>
    </item>
    <item>
      <title>🗿 Why Humans Invented HTML - Learning Frontend Deeply - Part 1</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Tue, 02 Feb 2021 20:18:28 +0000</pubDate>
      <link>https://dev.to/alimobasheri/why-humans-invented-html-learning-frontend-deeply-part-1-h1l</link>
      <guid>https://dev.to/alimobasheri/why-humans-invented-html-learning-frontend-deeply-part-1-h1l</guid>
      <description>&lt;h2&gt;
  
  
  Why Should You Read This?
&lt;/h2&gt;

&lt;p&gt;If you have ever watched Kubrick’s &lt;strong&gt;2001: A Space Odyssey&lt;/strong&gt;, you should’ve noticed its popular cutscene, in which an ape-man throws a bone into the sky and when the bone reaches it’s climax, it cuts into a scene in which a spacecraft is accelerating in the space.&lt;/p&gt;

&lt;p&gt;It contains an indirect and heroic message: “The same ape-man who threw a bone into the sky, out of joy and curiosity, was also the same man who sent spaceships to the outer space.”&lt;/p&gt;

&lt;p&gt;It’s the idea of evolution. How humans learnt to develop better solutions and tools, based on their past discoveries and knowledges.&lt;/p&gt;

&lt;p&gt;But how can all this stuff be related to HTML? We’re living in an age, where the web is rocking! There are web apps, websites, blogs, e-commerce stores, social medias, web games and much more. How could such a thing be related to an ape-man and his bone? The answer lies in how and for what purpose and based on what concepts, previously developed by humans, HTML was introduced.&lt;/p&gt;

&lt;p&gt;And this is what we’re going to learn about in this post. Fasten your seatbelts as we’re going to have a blazing journey throughout the history of HTML and the World Wide Web foundation! &lt;/p&gt;

&lt;p&gt;
  ❓ How Can This Article Be Helpful To You?
  &lt;ul&gt;
&lt;li&gt;This article is not a tutorial or doesn't try to teach you writing HTML. Of course in the future parts of this series, codes and tutorials will be added to the content too. 
But for now I would just like to demonstrate a deep picture of HTML and its purpose and foundation.
If you've just started to learn web development, it can be a good help for you. And if you're currently an experienced frontend developer, it can extend your view on the web development environment.
&lt;/li&gt;
&lt;/ul&gt;




&lt;/p&gt;
&lt;p&gt;
  ❓ Why Am I Publishing This Series?
  &lt;ul&gt;
&lt;li&gt;Because after I had succeeded in an interview but had lost the job in a week, I was motivated to learn everything related to frontend, deeply and bit by bit. In this series I will share with you the experience and the knowledge I gather along this journey.
You can read more in this article: &lt;a href="https://dev.to/alimobasheri/why-i-succeeded-in-the-interview-but-lost-the-job-gej"&gt;Why I Succeeded In The Interview But Lost The Job!&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;/p&gt;
&lt;h3&gt;
  
  
  Before We Start...
&lt;/h3&gt;

&lt;p&gt;Question: Like everything made by humans, HTML too has been developed in order to overcome a challenge. But what was the challenge and how could HTML specifically help to solve it?&lt;/p&gt;

&lt;p&gt;The short answer is that: HTML is the human solution for sharing documents. It's a Markup Language that simplifies the process of structuring documents that can be shared on the context of the World Wide Web.&lt;/p&gt;

&lt;p&gt;But the long answer is something more thorough. We should first learn how HTML was developed...&lt;/p&gt;
&lt;h2&gt;
  
  
  👨‍🔬 Born In A Physics Laboratory❕
&lt;/h2&gt;

&lt;p&gt;No. HTML wasn't meant to create webpages by using the energy released from atom excitation. But it was founded by Sir Tim Berners-Lee, who was in a position in the computing services section of CERN (the European Laboratory for Particle Physics in Geneva, Switzerland) in order to ease the process of document transportation. &lt;/p&gt;

&lt;p&gt;
  ❓ What made Tim feel the urge to create HTML? (click to expand)
  &lt;ul&gt;
&lt;li&gt;&lt;p&gt;CERN was a center for the gatherings of top physicists where they discussed and exchanged revolutionary ideas about things that can evolve and revolute the life on earth.&lt;br&gt;
Thus we can conclude that documents, like official ones or articles or manuscripts or etc. had a great usage in their gatherings.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But what if they could get rid of all the hassle of accessibility to these documents and save their valuable times for thinking on theories that could end human life on the earth with an easy swipe of particles, instead of waiting to reach Geneva and discuss for long hours with other scientists who would stubbornly never accept their theories?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;/p&gt;


&lt;p&gt;Hence Tim came up with the idea of a kind of a Hypertext which could behave like a paper document and let the reader navigate the pages and the contents by clicking on what became to be known as Hyperlinks. And all of these documents were going to be transferred through HTTP (HyperText Transfer Protocol), which helped the physicists access them around the world by having an internet connection. &lt;/p&gt;

&lt;p&gt;Surely, the internet we're talking about was different from what it looks today and the initial web browsers and webpages had many differences. Tim's idea for the HTML Document Type Definition was a blend of SGML and Hypertext. But what are these two? Let's find out together.&lt;/p&gt;
&lt;h3&gt;
  
  
  🦸‍♂️ Hypertext: Texts That Only Superman Can Read
&lt;/h3&gt;

&lt;p&gt;In defining Hypertext we should note that the word "Hyper" isn't used in the sense of a very long text or something that is about 1 million of words. Of course the internet and the Hypertext are useful tools that facilitate the access to billions of pages of documents, but Hypertext specifically means: "Documents that contain special anchors to other documents. These anchors can be accessed via mouse click or keypress or touch screens. Activating these anchors would make the browser jump to another document."&lt;/p&gt;

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

  The Anchor Tag ⚓ (click to expand)
  &lt;code&gt;&amp;lt;a href="http://example.com"&amp;gt;Link Text&amp;lt;/a&amp;gt;&lt;/code&gt;: The &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tag is one of the main HTML tags. The word &lt;code&gt;a&lt;/code&gt; is the first letter of 'Anchor'. And it accepts an attribute called href which stands for Hypertext Reference and it's value is the destination to which the browser must jump, upon the anchor's activation. This tag will render a link on the Hypertext document, whose address is the href's value.


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

&lt;p&gt;The Hypertext terminology had been known among the academics since the 1940s. But it wasn't until the first graphical PCs came to life, that Hypertexts could become a reality.&lt;br&gt;
The development of Hypertexts on PCs included document files which consisted of buttons by clicking on which, the system decided what content had to be displayed. Those were buttons like &lt;code&gt;next page&lt;/code&gt; and &lt;code&gt;previous page&lt;/code&gt; or &lt;code&gt;Chapter Titles&lt;/code&gt; in a table of contents. &lt;/p&gt;

&lt;p&gt;Yet these Hypertexts only worked on a PC and couldn't access documents on other PCs on the other side of the world. For this, Tim decided to implement &lt;strong&gt;HTTP&lt;/strong&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explaining what HTTP is and how it works, is out of the scope of this article. We will learn about it in some future articles of this series where we shall read about ajax.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then let's see what a Markup Language is and how SGML inspired Tim.&lt;/p&gt;
&lt;h3&gt;
  
  
  🧾 Markup Language: How To Define Elements In A Document
&lt;/h3&gt;

&lt;p&gt;I have many times come across people on the internet who debated whether HTML is a Programming Language or not. To me the answer can be found as easily as looking up it's name.&lt;br&gt;
HTML stands for &lt;strong&gt;Hypertext Markup Language&lt;/strong&gt;. Well, the name itself doesn't claim it's a programming language but instead it's a markup one. A markup language is a human-readable text that uses tags to define elements within a document. &lt;/p&gt;

&lt;p&gt;For a better understanding you can have a look at the current post. There's a title which says 'Why Humans Invented HTML'. Now let's suppose that we need to print a copy of this post and we fancy the title to be printed on the top-left corner of the page. The position is set beforehand in the printer, but the printer needs to know what the title is. Instead of passing the title to the printer separately, we define it in the document itself. But how?&lt;br&gt;
That's where the Markup Language comes to our rescue. &lt;br&gt;
In the text containing the Markup Language we define a title tag and pass it the title we desire. The printer, while parsing the text, would store the value for the title and print it in the correct position.&lt;br&gt;
Here's an 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;title&amp;gt;&lt;/span&gt;Why Humans Invented HTML&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example uses HTML tags. But since there are different Markup Languages, we could use any of them in their own respective environment.&lt;br&gt;
The following is an example of the Scribe Markup Language:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Title(Why Humans Invented HTML)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now that we have a good idea of how a Markup Language defines elements in a document, let's find out what SGML is.&lt;/p&gt;

&lt;h3&gt;
  
  
  📋 SGML: The Way Markup Languages Should Be Defined
&lt;/h3&gt;

&lt;p&gt;SGML stands for &lt;strong&gt;'Standard Generalized Markup Language'&lt;/strong&gt;. It's not a Markup Language in itself, instead it's a standard registered by ISO (International Standard Organization) which defines the standards for a Markup language.&lt;/p&gt;

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

  How can SGML be useful?
  &lt;p&gt;If new Markup Languages were about to be developed, this standard can be applied to them in order to make the Markup language supported on different platforms. Since the Markup languages based on this standard behave close to each other and obey the same rules, they can easily be ported to different formats and be printed easily. That's how HTML can easily be transformed into PDFs, EPUBs, XML and etc.&lt;/p&gt;

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

&lt;p&gt;The SGML itself was developed based on GML &lt;strong&gt;(Generalized Markup Language)&lt;/strong&gt;, earlier developed by IBM for it's printer devices. A document's structure in GML might look 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;:h1.The Title Goes Here!
:p.A paragraph about the content.
:h2.A title for a list.
:ol.
:li.List Item 1.
:li.List Item 2.
:eol.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

  What's the ':ol' Markup?
  &lt;p&gt;❗ Notice how &lt;code&gt;ol&lt;/code&gt; (ordered list) is used to start an ordered list and &lt;code&gt;eol&lt;/code&gt; (end ordered list) is used to declare that the list element ends there.&lt;/p&gt;

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

&lt;p&gt;The Markup would print the following document:&lt;/p&gt;




&lt;h4&gt;
  
  
  The Title Goes Here!
&lt;/h4&gt;

&lt;p&gt;A paragraph about the content.&lt;/p&gt;

&lt;h5&gt;
  
  
  A title for a list.
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;List Item 1.&lt;/li&gt;
&lt;li&gt;List Item 2.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;SGML replaced &lt;code&gt;:tagname. :etagname.&lt;/code&gt; with &lt;code&gt;&amp;lt;tagname&amp;gt;&amp;lt;/tagname&amp;gt;&lt;/code&gt;, which was also implemented into HTML.&lt;/p&gt;

&lt;p&gt;Now a new question pops up. How did HTML implement SGML?&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ HTML Comes To Life.
&lt;/h3&gt;

&lt;p&gt;When Sir Burners-Lee, first began to publicize his idea about sharing Hypertexts through the internet, it became well known among the researchers who taught it would become the main communication tool in the future.&lt;/p&gt;

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

  What did companies think about the HTTP at first? (click to expand)
  &lt;li&gt;The big companies like HP or IBM saw it useful only for the researchers who wanted to share their documents and make referencing simpler. To them, the communication future depended heavily on the telephone companies and the solutions they could find for its development.&lt;/li&gt;
&lt;li&gt;Well as it turns out they were wrong. Right now the WWW is the home to billions of websites, which nowadays are about more than just the research and the official documents.&lt;/li&gt;

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

&lt;p&gt;But even so, Tim was alone at first. He had to use his own computer abilities to bring his concepts into reality. He blended other concepts like Hypertexts, SGML and etc. and also developed his own sharing protocol which is HTTP. Then he was able to gather small teams and develop the world of communication!&lt;/p&gt;

&lt;p&gt;But what did the HTML look like at the beginning?&lt;/p&gt;

&lt;h4&gt;
  
  
  HTML: The First Version.
&lt;/h4&gt;

&lt;p&gt;For a real example you can visit this page: &lt;a href="http://info.cern.ch/hypertext/WWW/TheProject.html"&gt;The World Wide Web Project&lt;/a&gt;&lt;br&gt;
The above link will redirect you to the first page ever written in HTML. If you want to see how the page was Marked up, you can do so by right clicking on the browser page and selecting 'View Page Source', or other options like this.&lt;/p&gt;

&lt;p&gt;We can have a close look at it's source:&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;HEADER&amp;gt;&lt;/span&gt;
  ...
&lt;span class="nt"&gt;&amp;lt;/HEADER&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;/BODY&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The source as you can observe consists of human-readable tags. There are two main tags: &lt;code&gt;&amp;lt;HEADER&amp;gt;&lt;/code&gt; &amp;amp; &lt;code&gt;&amp;lt;BODY&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Now that we've learnt that a Markup language tries to stimulate a paper document, we can easily understand what these two tags are about. They define the two main parts of a document: 'Details' &amp;amp; 'Contents'. It's like many document folders you can find in offices and schools and elsewhere. Chances are you have yourself written such a document too.&lt;/p&gt;

&lt;p&gt;
  What does 'Header' really do?(click to learn)
  &lt;ul&gt;
&lt;li&gt;The details that the &lt;code&gt;&amp;lt;HEADER&amp;gt;&lt;/code&gt; tag gives are different from what we need to know about a paper document. The &lt;code&gt;&amp;lt;HEADER&amp;gt;&lt;/code&gt; part actually isn't meant to be read by humans. It contains information that a browser might find useful. Like what? Like a 'title' or a 'character set'.&lt;/li&gt;
&lt;/ul&gt;




&lt;/p&gt;
&lt;p&gt;Let's zoom into the &lt;code&gt;&amp;lt;HEADER&amp;gt;&lt;/code&gt; tag:&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;HEADER&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;TITLE&amp;gt;&lt;/span&gt;The World Wide Web project&lt;span class="nt"&gt;&amp;lt;/TITLE&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;NEXTID&lt;/span&gt; &lt;span class="na"&gt;N=&lt;/span&gt;&lt;span class="s"&gt;"55"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/HEADER&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since this is a very elementary version of HTML, just two tags have been defined inside &lt;code&gt;&amp;lt;HEADER&amp;gt;&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;One of them is title, which is simply a title or a name given to the current document. Since browsers have to inform the user about what they are looking at; the title is what the browser, shows at the top most position. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The title is one of the most important elements in a HTML document, based on the fact that if no title is defined, the user must rely on the HTTP Address URL to identify what document he/she's looking at.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;NEXTID&amp;gt;&lt;/code&gt; tag was used in the elementary HTML versions and produced by the &lt;strong&gt;Next&lt;/strong&gt; HTML editor to generate &lt;code&gt;name&lt;/code&gt; attributes for &lt;code&gt;Anchor&lt;/code&gt; tags. It's a long-deprecated tag and honestly I myself don’t perfectly know how it was used. 😅 It's not used anymore.&lt;/p&gt;

&lt;h5&gt;
  
  
  How can a deep understanding of HTML's development help you in developing modern web?
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;I can guess what you might be thinking by now: All we've learnt thus far, is how Markup languages have been developed and resulted in HTML's creation. But the facts about documents and their paper-like structures might seem irrelevant to how the web environment functions and looks nowadays. &lt;/li&gt;
&lt;li&gt;But that's the trick about it. Nothing has really changed. Everything you see on the web, from the HTML5 games to the Progressive Web Apps to the smoothly maintained animations and SVG graphics, all this and more is built upon document structure concept. This concept is known as &lt;strong&gt;Document Object Model&lt;/strong&gt; or &lt;strong&gt;DOM&lt;/strong&gt;, which I will write about in the next part of this series. &lt;/li&gt;
&lt;li&gt;I know reading this might seem boring and long and time consuming, but I can bet that by gathering this knowledge you can dive deeper into HTML5 Semantic Tags, Best CSS Layout Practices, SEO and How a Search Engine will treat and analyze your web application's &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;, JSX's data pipeline and how a Virtual DOM can become real and many more things.&lt;/li&gt;
&lt;li&gt;Believe me, these are all based upon the simple Hypertext idea 😊.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that you're again full of energy and motivated to search for the deep roots of HTML, let's see what an HTML document &lt;code&gt;BODY&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;This is how the &lt;code&gt;&amp;lt;BODY&amp;gt;&lt;/code&gt; part is written in the example above:&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;H1&amp;gt;&lt;/span&gt;World Wide Web&lt;span class="nt"&gt;&amp;lt;/H1&amp;gt;&lt;/span&gt;The WorldWideWeb (W3) is a wide-area&lt;span class="nt"&gt;&amp;lt;A&lt;/span&gt; &lt;span class="na"&gt;NAME=&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt; &lt;span class="na"&gt;HREF=&lt;/span&gt;&lt;span class="s"&gt;"WhatIs.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; 
hypermedia&lt;span class="nt"&gt;&amp;lt;/A&amp;gt;&lt;/span&gt;
  ...
  &lt;span class="nt"&gt;&amp;lt;DL&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;DT&amp;gt;&amp;lt;A&lt;/span&gt; &lt;span class="na"&gt;NAME=&lt;/span&gt;&lt;span class="s"&gt;44&lt;/span&gt; &lt;span class="na"&gt;HREF=&lt;/span&gt;&lt;span class="s"&gt;"../DataSources/Top.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;What's out there?&lt;span class="nt"&gt;&amp;lt;/A&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;DD&amp;gt;&lt;/span&gt; Pointers to the
    world's online information,&lt;span class="nt"&gt;&amp;lt;A&lt;/span&gt; &lt;span class="na"&gt;NAME=&lt;/span&gt;&lt;span class="s"&gt;45&lt;/span&gt; &lt;span class="na"&gt;HREF=&lt;/span&gt;&lt;span class="s"&gt;"../DataSources/bySubject/Overview.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; 
    subjects&lt;span class="nt"&gt;&amp;lt;/A&amp;gt;&lt;/span&gt;
    , &lt;span class="nt"&gt;&amp;lt;A&lt;/span&gt; &lt;span class="na"&gt;NAME=&lt;/span&gt;&lt;span class="s"&gt;z54&lt;/span&gt; &lt;span class="na"&gt;HREF=&lt;/span&gt;&lt;span class="s"&gt;"../DataSources/WWW/Servers.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;W3 servers&lt;span class="nt"&gt;&amp;lt;/A&amp;gt;&lt;/span&gt;, etc.
    &lt;span class="nt"&gt;&amp;lt;DT&amp;gt;&amp;lt;A&lt;/span&gt; &lt;span class="na"&gt;NAME=&lt;/span&gt;&lt;span class="s"&gt;46&lt;/span&gt; &lt;span class="na"&gt;HREF=&lt;/span&gt;&lt;span class="s"&gt;"Help.html"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Help&lt;span class="nt"&gt;&amp;lt;/A&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;DD&amp;gt;&lt;/span&gt; on the browser you are using
    ...
  &lt;span class="nt"&gt;&amp;lt;/DL&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;What the body renders is a set of &lt;code&gt;elements&lt;/code&gt;. Each element is identified by using tags. What text you put inside a tag is called its &lt;code&gt;inner text&lt;/code&gt; and tags that you nest into it are called it’s &lt;code&gt;children&lt;/code&gt;. The main tag itself is called the &lt;code&gt;parent&lt;/code&gt;. &lt;br&gt;
The inner text is the part which is printed in the browser page.&lt;/p&gt;

&lt;p&gt;Question: But why should we use such sibling terms like children and parents for a HTML structure?&lt;br&gt;
Answer: Because the HTML Document Structure behaves in an inheriting way.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;inheritance&lt;/code&gt; concept becomes more important when you need to handle &lt;code&gt;layout&lt;/code&gt;, &lt;code&gt;positioning&lt;/code&gt; and &lt;code&gt;display&lt;/code&gt;. There's a good example in the above HTML markup which demonstrates how &lt;code&gt;inheritance&lt;/code&gt; helps in &lt;code&gt;layout&lt;/code&gt; designing.&lt;/p&gt;

&lt;p&gt;The tag &lt;code&gt;&amp;lt;DL&amp;gt;&lt;/code&gt; defines a &lt;code&gt;Description List&lt;/code&gt;. What a Description List demonstrates is a list of &lt;code&gt;items&lt;/code&gt; with each of them having a &lt;code&gt;description&lt;/code&gt; of their own.&lt;/p&gt;

&lt;p&gt;
  A good example for a Description List would be a dictionary. (click to read why)
  &lt;ul&gt;
&lt;li&gt;A dictionary document contains a list of words. And each of the words has got a definition of itself. &lt;/li&gt;
&lt;li&gt;The words are the &lt;code&gt;items&lt;/code&gt; and the definitions are the &lt;code&gt;description&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;/p&gt;
&lt;p&gt;The above example uses &lt;code&gt;&amp;lt;DL&amp;gt;&lt;/code&gt; to show a list of Hyperlinks. It can be said that each Hyperlink is written like a &lt;strong&gt;term&lt;/strong&gt;, which in turn is defined with a &lt;strong&gt;description&lt;/strong&gt; to tell the user about where the Hyperlink leads them to. This is the obvious purpose for the implementation of the &lt;code&gt;DL&lt;/code&gt; in this document. But there's also another use for it...&lt;/p&gt;

&lt;p&gt;It's &lt;code&gt;layout&lt;/code&gt;. If you're already familiar with web design and CSS, you'd know how CSS properties like Flexbox or Grid help with the layout. &lt;br&gt;
But the initial Hypertext project had defined no way of styling. And this was for good reason. Because Burners-lee anticipated the fact that HTML had to only contain document structure and markup, concluding that the styling logic shouldn't be implemented in HTML itself. This made HTML more flexible. One document structure could be styled in different ways, either by the user or the author or the browser.&lt;/p&gt;

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

  What is Flexbox or Grid? Hell, what is CSS? (click to read if you don't know)
  &lt;p&gt;CSS stands for &lt;strong&gt;Cascading Style Sheets&lt;/strong&gt;. A style sheet is a set of rules that apply styles to a document. And cascading means these rules act like cascades of water which override everything beneath themselves. 
&lt;/p&gt;


&lt;li&gt;Thus certain style rules can be changed or overridden in certain parts of a document. This property is different from HTML's inheritance behavior and will be covered in future parts of this series.&lt;/li&gt;
&lt;br&gt;
&lt;li&gt;Flexbox and Grid are some properties of CSS used for HTML layout. They align contents in a &lt;code&gt;list-like&lt;/code&gt; and &lt;code&gt;table-like&lt;/code&gt; way respectively.&lt;/li&gt;
&lt;br&gt;

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

&lt;p&gt;What this means is that the &lt;code&gt;description list&lt;/code&gt; element is used to give the printed (or the rendered) content some visual &lt;strong&gt;layout&lt;/strong&gt; and &lt;strong&gt;structure&lt;/strong&gt;. &lt;br&gt;
The structure is implemented by showing only one Hyperlink on each line and it's description in the next line. This structure makes it easier for the reader to read the document.&lt;/p&gt;

&lt;p&gt;I hope this example has demonstrated how the HTML documents were initially structured. Even the newest HTML versions follow many of these rules. So you can observe how and for what purposes HTML was developed.&lt;/p&gt;

&lt;h3&gt;
  
  
  And That's All For Now...!
&lt;/h3&gt;

&lt;p&gt;We've come a long way, describing many things, that you may have already known. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;But what I was trying to achieve by writing this long article, was to give you a fundamental sense of how the frontend technologies have been defined. &lt;/li&gt;
&lt;li&gt;In my opinion, it's all based on the document structure, whether it's about online video meetings or social applications, or graphical animations. &lt;/li&gt;
&lt;li&gt;Having a firm knowledge of the markup language and the inheritance methodology will surely help us throughout our journey to learn frontend deeply.&lt;/li&gt;
&lt;li&gt;One important thing about the DOM is the usage of the semantic tags. You've seen different kinds of these tags in this article. They include &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;h1&lt;/code&gt;, &lt;code&gt;list&lt;/code&gt; and etc. Such tags are semantic in the sense that they let the browser know what role their content has in the document. &lt;/li&gt;
&lt;li&gt;For example, a &lt;code&gt;h1&lt;/code&gt; tag means that its text is the most important title in the current document and surely its text contains some important and descriptive information about the current section.&lt;/li&gt;
&lt;li&gt;Learning semantic tags is important for SEO, layout and even mastering the declarative and component-based programming. And in the latest definitions of HTML, newer semantic tags have been introduced which play a key role in developing better frontend applications.&lt;/li&gt;
&lt;li&gt;In the upcoming parts we're going to use the knowledge gathered in this article to dive deeper into the sea of the frontend programming!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Feedbacks please!
&lt;/h3&gt;

&lt;p&gt;This is a long post, and it took me a while to write it down. And since I'm planning to extend it into a series I would like to know your opinions and ideas about it. &lt;/p&gt;

&lt;p&gt;And most important than all: Many things written in this post can be categorized as opiniated explanations. Therefore opposite or better explanations and theorizations might exist. If you have such ideas, please comment them.&lt;/p&gt;

&lt;p&gt;In the next part we'll be back with the DOM.&lt;/p&gt;

&lt;p&gt;Thanks for the reading! ❤️&lt;/p&gt;

</description>
      <category>html</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Why I Succeeded In The Interview But Lost The Job!</title>
      <dc:creator>MirAli Mobasheri</dc:creator>
      <pubDate>Mon, 04 Jan 2021 09:23:05 +0000</pubDate>
      <link>https://dev.to/alimobasheri/why-i-succeeded-in-the-interview-but-lost-the-job-gej</link>
      <guid>https://dev.to/alimobasheri/why-i-succeeded-in-the-interview-but-lost-the-job-gej</guid>
      <description>&lt;h2&gt;
  
  
  What did encourage me to write this post❓
&lt;/h2&gt;

&lt;p&gt;Two weeks ago I applied for 3 companies. The job offers were aimed at junior Front-end developers. Well, all of them accepted my resume and two of them interviewed me. One of them never told the results and the last one offered me the job.&lt;/p&gt;

&lt;p&gt;Yet I had to work for 3 experimental weeks and if they really liked the way I did my job, I would've been recruited.&lt;br&gt;
So I accepted the deal and began to work there and then after spending a week on the job I got rejected. Certainly I was paid for the one week work but in the end, right now I'm sitting jobless in front of my laptop, trying to describe to you why I succeeded and then failed.&lt;/p&gt;

&lt;p&gt;I've shared valuable advices with you in this post. &lt;br&gt;
But first of all I suppose you need to have some background of me.&lt;/p&gt;

&lt;h3&gt;
  
  
  Who am I?
&lt;/h3&gt;

&lt;p&gt;I am Ali and I'm a deaf. Deaf is the word that could describe me appropriately. Because it's not only a disability it's also a way of life.&lt;/p&gt;

&lt;p&gt;I live in Tehran, Iran and I'm 21 years old. I'm still studying at the university. My education field is Industrial Safety and I'm currently studying at the Shahid Beheshti University of Tehran.&lt;/p&gt;

&lt;p&gt;I think this short introduction is enough to let you have some basic knowledge of my condition. &lt;br&gt;
Now let's have a few words about how I entered the world of code.&lt;/p&gt;

&lt;h4&gt;
  
  
  How the journey began...
&lt;/h4&gt;

&lt;p&gt;It wasn't a stormy night in the middle of the winter. The journey had actually began since my youth. I am 21 years old at the moment and the first time I wrote my first line of HTML, there was no sign of it's fifth version.&lt;/p&gt;

&lt;p&gt;And then the HTML 5 and the CSS 3 were introduced. The excitement at the time was mainly about the HTML semantic tags and the CSS opacity and border-radius properties. Since then the languages have developed further more.&lt;/p&gt;

&lt;h4&gt;
  
  
  And then I got lost in the journey...
&lt;/h4&gt;

&lt;p&gt;Yes, I had written HTML a very long time ago. And I was even learning PHP 5 and MySQL. But it was never serious learning. It was out of curiosity and self-pleasure. So I left the learning hobby very soon in order to continue my school studies.&lt;/p&gt;

&lt;p&gt;But a day came, when nothing was like before. My left ear too, said goodbye to the hearing world to join my right one; which had gone deaf since my childhood.&lt;/p&gt;

&lt;h4&gt;
  
  
  Who said there were no stormy nights?
&lt;/h4&gt;

&lt;p&gt;Having a severe deafness, up to 90% in both ears, I was left in the world of solitude. Talking, socializing, learning and any normal activity that you could name, became tremendously hard for me.&lt;/p&gt;

&lt;p&gt;But for the next three years I was the top student in a normal high school. I found my way into the university and then, finally I returned to my old hobby. Programming.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is Bootstrap? Where did React.js come from? Really, ES6?
&lt;/h4&gt;

&lt;p&gt;Being absent for a long time, I knew nothing of the newest developments in the frontend world. It was like I had come out of a cave in which I had lived for a thousand years. &lt;/p&gt;

&lt;p&gt;Then I began to experiment with the new things and eventually I was on the road to become a front-end developer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Apply here, apply there, apply everywhere
&lt;/h3&gt;

&lt;p&gt;Three months have passed since the first time I really applied for a job. Then I had applied for 3 companies but none of them even accepted me. Why? you may ask, and my answer would be that my resume sucked.&lt;/p&gt;

&lt;p&gt;It was just a description of myself and the things I liked and how I was involved into programming. Aside that I had an open source sample project on Github which was deployed online. But the project was unfinished and it really had no great design. &lt;/p&gt;

&lt;p&gt;I had thought the code itself and the technologies that I had used in it would be enough. But it wasn't, so I didn't even get a call for an interview.&lt;/p&gt;

&lt;p&gt;About two months passed, and I got my first interviews. How?&lt;/p&gt;

&lt;h1&gt;
  
  
  How I Succeeded In The Interview.
&lt;/h1&gt;

&lt;h2&gt;
  
  
  # Point 1: The resume, the resume, the resume.
&lt;/h2&gt;

&lt;p&gt;I totally rewrote my resume. There were no descriptions of me. Just lists of what I had mastered and what I was currently learning and what I had had experience with. It went like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;I know JavaScript since my youth. I'm mastered at:
HTML5, CSS3, JS ES6, jQuery, Bootstrap, React.js, Redux.js, ...&lt;/li&gt;
&lt;li&gt;I'm currently learning:
Advanced Node.js, Express.js, ...&lt;/li&gt;
&lt;li&gt;I have experience with:
Python, Web-Scraping, Phaser.js 3, ...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Plus points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clean code&lt;/li&gt;
&lt;li&gt;Functional Programming&lt;/li&gt;
&lt;li&gt;High self-education and self-reliability: Top high school student, despite hearing problems&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;So as you can see, instead of writing sentences, describing myself, I gave quick facts about my abilities and specialties. This way the reader can quickly match my abilities with the requirements of the job.&lt;/p&gt;

&lt;p&gt;But the resume's description part wasn't the only difference. I also created more sample projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  # Point 2: Sample Projects, or Show Them What You Can Do.
&lt;/h2&gt;

&lt;p&gt;The second time I sent my resumes, I had three main sample projects. Every three of them were big projects, but unfinished. But the point here isn't the project itself, or its deployment and demo.&lt;/p&gt;

&lt;p&gt;What I did was to write proficient descriptions for every project in my Github account. In my resume I redirected the reviewer to the readme section of my repos and tried to attract their attention.&lt;/p&gt;

&lt;p&gt;What my Readmes included were a list of descriptions of the technologies I had used and their purpose. &lt;br&gt;
Such a description is what actually every reviewer is looking for. No one is going to review every line of your code. &lt;/p&gt;

&lt;p&gt;The fact that you can explain the tools you have used in your project and their purpose, will give the reviewer a good idea of the fact that you generally understand those tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  # Point 3: The Interview, or Let The Stress Out
&lt;/h2&gt;

&lt;p&gt;Because of my deafness and due to the Covid-19 spread I offered both of the interviewers to do the interview on a video call and type the questions at the same time.&lt;/p&gt;

&lt;p&gt;Since I couldn't hear their voice, they would type their questions for me and I would give my replies by speech. &lt;/p&gt;

&lt;p&gt;The most important part about job interviews is preparation.&lt;br&gt;
You should study the languages' and the libraries' references and have a good realization of their application. But...&lt;br&gt;
Remember that in an interview you are actually compared to other appliers. &lt;/p&gt;

&lt;p&gt;So you don't have to answer every question correctly. But answer every one of them with confidence. And if you don't know an answer, just confess it. Don't give random answers, as it would cause negative attention. &lt;/p&gt;

&lt;p&gt;And remember that your interview also demonstrates how you can share knowledge and collaborate in a team. Therefore don't get stressful. &lt;/p&gt;

&lt;p&gt;Thus there is no magic way to succeed in an interview, but the picture you give of yourself can ultimately lead to success. This can be achieved through a confident resume and interview.&lt;/p&gt;

&lt;p&gt;Yes, I managed to succeed in one of the interviews which was more generally about the core JS, CSS and HTML. &lt;/p&gt;

&lt;p&gt;I don't think my other interview was really very bad, but it was the kind of interview which required you to know every theoretical parts of React and Next.js or Node.js and etc. which I didn't really have a good grasp upon. I was never informed of the results.&lt;/p&gt;

&lt;p&gt;Accepted but not recruited.&lt;/p&gt;

&lt;h1&gt;
  
  
  How I Failed The Job
&lt;/h1&gt;

&lt;p&gt;The seventh night, after I returned from work, I received a text message which went on like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Mr. Mobasheri, hello:&lt;br&gt;
As was mentioned before in our skype chat, we always experiment our coworkers in an experimental duration. To be successful in our business we need an experienced and fast developer. But in the week that has passed we've concluded that we can't continue collaboration with you.&lt;br&gt;
We wish you the best in the path you have chosen...&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, there was nothing special I could do about it. But I thought it was my right to know why I was rejected. &lt;/p&gt;

&lt;p&gt;"What's wrong with my work?", I asked the senior programmer the next day, "I have done everything you required of me!"&lt;br&gt;
"What's wrong with your work?" He typed this on the WhatsApp web app, open on the screen in front of him (because of my hearing issue), "Let me tell you."&lt;/p&gt;

&lt;p&gt;And I thank him for his clean and direct reply. I never knew how much wrong I had gone the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  # Point 1: Clean Code, or You Don't Know JS
&lt;/h2&gt;

&lt;p&gt;I was informed that writing messy code was my biggest issue. But wait. Hadn't I written in my resume that clean code was one of my plus points? So how comes that I couldn't follow its rules in my own code?&lt;/p&gt;

&lt;p&gt;I had read the Clean Code book and have always tried to implement its rules in my programming. But it turns out that my problem was in React. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I didn't write clean React components. &lt;/li&gt;
&lt;li&gt;I didn't use React's core abilities to shorten the code.&lt;/li&gt;
&lt;li&gt;I had even written a long useEffect to manage routing states for a part of the site which used step by step navigation, while I could have achieved this using JS Objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But it wasn't only about React. In fact clean code isn't all about component names and Pure Functions. It's also about using the programming language's own methods to reduce code. At which I was again defeated.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There was a point at which I had to format Integers to prices. As an example: 25000 -&amp;gt; 25,000. Since the project was multilingual and both Persian and English formats of the price were required, I had chosen to write a complete function which would do the job. Using map and reduce and other array Methods, it consisted of 13 lines of code. But indeed I didn't need to use that. JavaScript has an in-built Method for Integers which is &lt;code&gt;toLocaleString()&lt;/code&gt; and that does the job perfectly. The only thing I needed to do was to pass it the lang abbreviation (English: 'en', Persian: 'fa').&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As simple as this I was rejected for not having a deep knowledge of everything about React and JS.&lt;/p&gt;

&lt;h2&gt;
  
  
  # Point 2: Responsive Design Strategies, or Understanding Every Bit Of HTML 5 and CSS 3
&lt;/h2&gt;

&lt;p&gt;God, I know responsive design! I know how to implement Flexbox. Even when during the interview I was asked how to create a Glassmorphic design, I replied that &lt;code&gt;backdrop-filter: blur&lt;/code&gt; is the right way and it was totally right.&lt;/p&gt;

&lt;p&gt;But the senior programmer told me that I had used strange ways to implement design in the Front-end.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I had wrapped many elements inside a &lt;code&gt;div&lt;/code&gt; tag. Instead I could use CSS properties directly on the elements themselves.&lt;/li&gt;
&lt;li&gt;I had used the percentage unit &lt;code&gt;%&lt;/code&gt; over and over again. I was told that the percentage unit isn't very predictable. And also that I had written rules like &lt;code&gt;padding: 30%&lt;/code&gt; for responsivity, which he said he had never seen before.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But why did this happen?&lt;/p&gt;

&lt;p&gt;The senior programmer who was also my interviewer told me that I had had the best interview results and that nobody had answered the questions as thoroughly as me, but he wondered why I wasn't so good in action.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I needed more experience and that was right.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But there was also another fact. He told me that the interview's questions were all the type of questions for which to be able to give a correct answer you had to have had some experience in the related matters. &lt;/p&gt;

&lt;p&gt;Well that's truly right. I was experienced enough to answer all those questions. But the interview questions mainly focused on the general matters. I'm a self-educated programmer. I have practiced everything by myself. So I might have solved the problems in ways which didn't look standard or clean.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I have learnt the technologies widely, but not deeply. I can create a web app or a website from the scratch using the famous tools. But I'm not fast enough. That's because I haven't focused enough on the design patterns. I haven't learnt the languages and the libraries deep enough.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And most importantly I need to practice every small aspect of the technologies and the languages used in the Front-end development. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's the goal I set for myself yesterday when I returned home from the office. &lt;/p&gt;

&lt;p&gt;Got paid but failed:(&lt;/p&gt;

&lt;h1&gt;
  
  
  What To Expect In The Future?
&lt;/h1&gt;

&lt;p&gt;I hope this post would be helpful for the readers.&lt;/p&gt;

&lt;p&gt;But if you have really read through my long story, I would be glad to announce that I want to start publishing new series of articles on dev.to. &lt;/p&gt;

&lt;p&gt;My goal is to write about clean code in React.js and deep learning of JS, HTML, CSS, React.js and etc.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Journey Has Just began.
&lt;/h4&gt;

&lt;p&gt;Failed but not done 😏&lt;/p&gt;

</description>
      <category>interview</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
