<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Faisal </title>
    <description>The latest articles on Forem by Faisal  (@thefaisal).</description>
    <link>https://forem.com/thefaisal</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%2F1022522%2F986c4b84-c8ee-4fc1-969e-2d9752d91464.jpg</url>
      <title>Forem: Faisal </title>
      <link>https://forem.com/thefaisal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thefaisal"/>
    <language>en</language>
    <item>
      <title>JavaScript Coercion : Beyond Basics</title>
      <dc:creator>Faisal </dc:creator>
      <pubDate>Sat, 02 Dec 2023 15:03:14 +0000</pubDate>
      <link>https://forem.com/thefaisal/javascript-coercion-beyond-basics-al7</link>
      <guid>https://forem.com/thefaisal/javascript-coercion-beyond-basics-al7</guid>
      <description>&lt;p&gt;In JavaScript, we often see implicit type conversion in our code which occurs due to abstract operation. In JS, we use the term coercion for what's commonly known as type conversion. When we consider conversion and coercion, it's best to see them as interchangeable, especially in the context of JavaScript.&lt;/p&gt;

&lt;p&gt;Coercion is a weird topic in javascript, that is why many tends to ignore this topic. However, we can't ignore something that behaves counterintuitively. We will start by exploring how abstract operations and coercion occur implicitly. Following that, we'll discuss why they are important.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here one thing to note that, abstract operations are not like regular functions that you explicitly call in your code. Instead, they represent conceptual operations that help describe the behavior of certain language features.&lt;/p&gt;

&lt;p&gt;While there may be internal methods or mechanisms within the JavaScript engine that handle the tasks associated with abstract operations, the key idea is that these operations are more about defining behavior and functionality at a higher level of abstraction.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  ToPrimitive Operation
&lt;/h2&gt;

&lt;p&gt;The abstract operation that we will talk about first is called &lt;code&gt;ToPrimitive&lt;/code&gt; or primitive coercion process. The &lt;code&gt;ToPrimitive&lt;/code&gt; operation converts a value to a primitive value, either a string, number, or a default value. It is often invoked implicitly by JavaScript when an object is used in a context where a primitive value is expected.&lt;/p&gt;

&lt;p&gt;Let's say we are performing an operation where it requires a value to be primitive. Now, if we don't have a primitive in there, we need to turn that into a primitive.&lt;/p&gt;

&lt;p&gt;If we have a non-primitive entity that needs to be transformed into a primitive one, conceptually, what it must follow is a series of algorithmic steps. This set of steps is referred to as &lt;code&gt;ToPrimitive&lt;/code&gt;, almost as if it were a function that could be called.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ToPrimitive&lt;/code&gt; operation takes an optional type &lt;code&gt;hint&lt;/code&gt;. The hint helps to figure out what it needs to be converted to. Let's say if we are doing a numeric operation and if there needs to run the &lt;code&gt;ToPrimitive&lt;/code&gt; operation, the hint will be something like this - 'I would like it to be a number'. It's just an implicit process where JavaScript understands what type it needs to be. If we do something string-based, it sends a hint as a string. These are the two basic hints. And if it can't figure out the hint, it returns whatever the best primitive value it can return in that context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One important thing is that -&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JavaScript algorithms inherently follow a recursive pattern. For example, if what returns from &lt;code&gt;ToPrimitive&lt;/code&gt; isn't a primitive type value but instead another non-primitive thing, &lt;code&gt;ToPrimitive&lt;/code&gt; will be called again. It will keep getting invoked until we obtain an actual primitive. If it can't return a primitive value, it eventually results in an error.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;ToPrimitive&lt;/code&gt; operation relies on two methods - &lt;code&gt;valueOf&lt;/code&gt; and &lt;code&gt;toString&lt;/code&gt;. These can be available on any non-primitive value in JavaScript, be it an object, function, array, or any other non-primitive type. These two methods/functions play a crucial role in the process of converting non-primitive values to primitive ones.&lt;/p&gt;

&lt;p&gt;Remember, &lt;code&gt;ToPrimitive&lt;/code&gt; operation takes an optional hint.&lt;/p&gt;

&lt;p&gt;Now if the hint is number, then &lt;code&gt;ToPrimitive&lt;/code&gt; invokes the &lt;code&gt;valueOf&lt;/code&gt; method first and see what it returns. If &lt;code&gt;valueOf&lt;/code&gt; method gives a primitive, then we're done. However, if it doesn't give a primitive or if the method itself doesn't exist, then it tries the second method - &lt;code&gt;toString&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;toString&lt;/code&gt; method also fails to give a primitive, it usually leads to an error.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JavaScript calls the &lt;code&gt;toString&lt;/code&gt; method to convert an non primitive to a primitive value. We rarely need to invoke the &lt;code&gt;toString&lt;/code&gt; method ourselves; JavaScript automatically invokes it when encountering an object where a primitive value is expected. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString"&gt;MDN&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the hint is string, still both &lt;code&gt;valueOf&lt;/code&gt; and &lt;code&gt;toString&lt;/code&gt; method occurs, but in reverse order. In terms of string hint, if it &lt;code&gt;ToPrimitive&lt;/code&gt; operation tries to make a non primitive into a primitive, &lt;code&gt;toString&lt;/code&gt; method invoke first. Then, if it gives us a primitive like a string, we will just use that.&lt;/p&gt;

&lt;p&gt;So, no matter what the hint is, if we are trying to use something that is not primitive where it needs to be primitive, like in some math or concatenation, then it goes through &lt;code&gt;ToPrimitive&lt;/code&gt; algorithm and it ends up either invoking the &lt;code&gt;valueOf&lt;/code&gt; method or &lt;code&gt;toString&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Diving deep into valueOf method
&lt;/h2&gt;

&lt;p&gt;Let's take an object - &lt;code&gt;const obj = {value: 4}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, &lt;code&gt;obj + 3&lt;/code&gt; returns us &lt;code&gt;'[object Object]3'&lt;/code&gt; as output. But, shouldn't it be 7? Since the object's value is giving a number hint, it should already be turned into a number, right? Then, why is it behaving this way?&lt;/p&gt;

&lt;p&gt;To figure out that answer, we need to understand that, under the hood, JavaScript is still running the &lt;code&gt;ToPrimitive&lt;/code&gt; operation on our written &lt;code&gt;obj&lt;/code&gt; to convert it to work in primitive operations - like adding value. And, since it's taking a number as a hint - as we know, it is running the &lt;code&gt;valueOf&lt;/code&gt; method first. But, the problem is, the default &lt;code&gt;valueOf&lt;/code&gt; method of JavaScript, which gets inherited from &lt;code&gt;Object.prototype&lt;/code&gt;, is basically useless in this case because the &lt;code&gt;valueOf&lt;/code&gt; method inherited from &lt;code&gt;Object.prototype&lt;/code&gt; returns the object itself (&lt;code&gt;this&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;And, since the &lt;code&gt;ToPrimitive&lt;/code&gt; algorithm is recursive, it runs again, and sees that the &lt;code&gt;valueOf&lt;/code&gt; method is not working, so, it falls back into the &lt;code&gt;toString&lt;/code&gt; method. And when the &lt;code&gt;toString&lt;/code&gt; method runs on any object type, it returns - &lt;code&gt;"[object Object]"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, our object basically is turning into a string first which is &lt;code&gt;"[object Object]"&lt;/code&gt;, Then, when we use the &lt;code&gt;+&lt;/code&gt; operator between two &lt;code&gt;string&lt;/code&gt; types, it just concatenates them.&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;obj&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[object Object]&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[object Object]3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Many built-in objects override the &lt;code&gt;valueOf&lt;/code&gt; method to return an appropriate primitive value. When you create a custom object, you can override &lt;code&gt;valueOf&lt;/code&gt; to call a custom method so that your custom object can be converted to a primitive value when needed.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;value&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="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// {value: 4}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is returning the object itself(&lt;code&gt;this&lt;/code&gt;). But, you can create a function to be called in place of the default &lt;code&gt;valueOf&lt;/code&gt; method. Your function should take no arguments, since it won't be passed any when called during type conversion.&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;value&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="na"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&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="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, it is just returning the value. And, if we try to execute something where our written &lt;code&gt;obj&lt;/code&gt; will need to be a primitive type, we will see that it will behave as expected -&lt;/p&gt;

&lt;p&gt;&lt;code&gt;obj + 3&lt;/code&gt; will return 7.&lt;/p&gt;

&lt;p&gt;Why? Because, since the adding operation is invoking the &lt;code&gt;ToPrimitive&lt;/code&gt; operation with a &lt;code&gt;number&lt;/code&gt; hint, so, the &lt;code&gt;valueOf&lt;/code&gt; method is running first. But, now, our &lt;code&gt;obj&lt;/code&gt; has its own custom &lt;code&gt;valueOf&lt;/code&gt; method, so it's not executing the inherited &lt;code&gt;valueOf&lt;/code&gt; method from &lt;code&gt;Object.prototype&lt;/code&gt; which returns &lt;code&gt;this&lt;/code&gt; object itself. Instead, for our custom &lt;code&gt;valueOf&lt;/code&gt; method, it's giving us the actual number form of the value, since we are getting a primitive type already, it's not falling into the &lt;code&gt;toString&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  ToString Operation
&lt;/h2&gt;

&lt;p&gt;Here are some values and their corresponding string values after performing the &lt;code&gt;ToString&lt;/code&gt; operation on them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;null&lt;/code&gt; =&amp;gt; "null"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;undefined&lt;/code&gt; =&amp;gt; "undefined"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;true&lt;/code&gt; =&amp;gt; "true"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;false&lt;/code&gt; =&amp;gt; "false"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;3.14&lt;/code&gt; =&amp;gt; "3.14"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; =&amp;gt; "0"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-0&lt;/code&gt; =&amp;gt; "0"
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;ToString&lt;/code&gt; operation is executed during abstract operations involving implicit type conversion.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;ToString&lt;/code&gt; is applied to object types like arrays, it returns values as demonstrated in the following examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[]&lt;/code&gt; =&amp;gt; ""&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[1, 2, 3]&lt;/code&gt; =&amp;gt; "1,2,3"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[null, undefined]&lt;/code&gt; =&amp;gt; ","&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[ [ [ ], [ ] ], [ ] ]&lt;/code&gt; =&amp;gt; ",,"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[,,,]&lt;/code&gt; =&amp;gt; ",,,"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For objects:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;{}&lt;/code&gt; =&amp;gt; "[object Object]"&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;{a: 2}&lt;/code&gt; =&amp;gt; "[object Object]"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can modify the default behavior of &lt;code&gt;toString&lt;/code&gt; by implementing a custom &lt;code&gt;toString&lt;/code&gt; method in our object. It will be called when needed:&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="nf"&gt;toString&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;A&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ToNumber Operation
&lt;/h2&gt;

&lt;p&gt;There are a lot of corner cases involved in terms of ToNumber.&lt;/p&gt;

&lt;p&gt;If we need to do something numeric and we don't have a number, JS invokes the ToNumber abstract operation. Some conversions are pretty straightforward, while others are counterintuitive.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;"" =&amp;gt; 0&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An empty string returns 0. Empty string should be NaN, right? Empty means there is nothing. How can nothing be 0? That's weird. It's not only a weird case in JavaScript; it creates a lot of other problems in terms of [[Coercion]] in JS.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;&lt;code&gt;" 003 " =&amp;gt; 3&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there are any empty spaces, JS removes those and returns the corresponding number.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;&lt;strong&gt;Others are pretty straightforward:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;"0" =&amp;gt; 0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"-0" =&amp;gt; -0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"3.14" =&amp;gt; 3.14&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"0.0" =&amp;gt; 0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;"." =&amp;gt; NaN&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"." turns into NaN, which is as it should.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;&lt;code&gt;"0xaf" =&amp;gt; 175&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hexadecimal string turns into their corresponding numbers.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;false =&amp;gt; 0&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;code&gt;true =&amp;gt; 1&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Within the context of programming, maybe it's a sensible thing to turn false and true into 0 and 1, as that's how computers treat them. But, it creates an issue. false and true should turn into NaN. We will see the reason later when we discuss the corner cases.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;null =&amp;gt; 0&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;&lt;code&gt;undefined =&amp;gt; NaN&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Null and undefined both should be NaN, but one turns into 0, and another turns into NaN. That's interesting. For those who think that converting null uses the valueOf method of Object because null is an object, one interesting thing is that null doesn't inherit methods from Object.prototype.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h3&gt;
  
  
  ToNumber Operation on Non-Primitive
&lt;/h3&gt;

&lt;p&gt;When we run ToNumber on a non-primitive or object, it evokes the &lt;code&gt;ToPrimitive&lt;/code&gt; operation with the number hint. In &lt;code&gt;ToPrimitive&lt;/code&gt; operation, it first runs the &lt;code&gt;valueOf&lt;/code&gt; method, which is inherited from the Object.prototype by default, and it returns the object itself (&lt;code&gt;this&lt;/code&gt;). JS ignores that, and it falls into the &lt;code&gt;toString&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Since it falls into &lt;code&gt;tpString&lt;/code&gt;, no matter if our hint is a number or not, the value turns into a string. So, we can think of the numberification of an object as the stringification of it.&lt;/p&gt;

&lt;p&gt;There are cases where we want something to be a primitive number, and we can end up with a primitive string for the internal process of &lt;code&gt;valueOf&lt;/code&gt;. We need to be aware of that.&lt;/p&gt;

&lt;p&gt;If it's empty like &lt;code&gt;[]&lt;/code&gt; or &lt;code&gt;{}&lt;/code&gt;, then the same things happen. It evokes the &lt;code&gt;ToPrimitive&lt;/code&gt;, and the &lt;code&gt;valueOf&lt;/code&gt; returns the empty parenthesis - &lt;code&gt;[]&lt;/code&gt; or &lt;code&gt;{}&lt;/code&gt;, then it falls into &lt;code&gt;toString&lt;/code&gt; as usual.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some more ToNumber operations in terms of arrays:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[""]&lt;/code&gt; =&amp;gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;["0"]&lt;/code&gt; =&amp;gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;["-0"]&lt;/code&gt; =&amp;gt; -0&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[null]&lt;/code&gt; =&amp;gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[undefined]&lt;/code&gt; =&amp;gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[1,2,3]&lt;/code&gt; =&amp;gt; NaN&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[[[]]]&lt;/code&gt; =&amp;gt; 0&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Few important things to notice here:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;[""]&lt;/code&gt; falls into ToString, and then it returns an empty string. If &lt;code&gt;ToNumber&lt;/code&gt; tries to convert an empty string, it converts it to 0, which is our old problem as we saw earlier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Similar things happen for &lt;code&gt;[[[]]]&lt;/code&gt; - first, it turns into "" by toString, then, it converts into a number, which is 0. Emptiness shouldn't be 0 in this case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We see that &lt;code&gt;[null]&lt;/code&gt; and &lt;code&gt;[undefined]&lt;/code&gt; return 0 in the ToNumber operation, which doesn't make sense. We have seen different behavior in their case when we have tried to convert primitive null and undefined. But, in this case, they are returning an identical value, which is 0. Why? In this case, this is a non-primitive thing in both cases, and as it's non-primitive, &lt;code&gt;ToPrimitive&lt;/code&gt; evokes, and the valueOf method runs first. It returns &lt;code&gt;this&lt;/code&gt;, then it falls into the &lt;code&gt;toString&lt;/code&gt; method, and remember what &lt;code&gt;toString&lt;/code&gt; does on a non-primitive with null or undefined? It turns into an empty string. And then, that empty string turns into 0. Our root problem again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;{..}&lt;/code&gt; =&amp;gt; &lt;code&gt;NaN&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it's an object {}, then the same thing happens. &lt;code&gt;ToPrimitive&lt;/code&gt; evokes, then it falls into toString. ToString returns "[object Object]", which is definitely not a representation of a number, so it returns &lt;code&gt;NaN&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;But, if we add our custom &lt;code&gt;valueOf&lt;/code&gt; method, it doesn't fall into toString, and it returns what we want to be returned.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h2&gt;
  
  
  ToBoolean Operation
&lt;/h2&gt;

&lt;p&gt;This is pretty straightforward. We just need to remember all the falsy values, which will always return false if we try to run ToBoolean operation on them. Everything else returns true.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Falsy values&lt;/strong&gt;: "", 0, -0, null, NaN, false, undefined &lt;strong&gt;Truthy values&lt;/strong&gt;: Everything else.&lt;/p&gt;

&lt;p&gt;One thing is important to notice here:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When we try to convert anything to Boolean, it doesn't evoke any other operation under the hood, like &lt;code&gt;ToNumber&lt;/code&gt;. It just checks its truthiness or falsiness.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's why if we convert [ ] to a boolean, it returns true, and if we convert "" to a boolean, it returns false. Because an empty string is a falsy value, [ ] is not.&lt;/p&gt;

&lt;p&gt;If it would evoke the &lt;code&gt;toString&lt;/code&gt; method under the hood, that would convert [] to an empty string, and then we would get false because of that empty string. But, in this case, that is not happening.&lt;/p&gt;

&lt;h2&gt;
  
  
  Corner Cases
&lt;/h2&gt;

&lt;p&gt;Every language has type conversion. We can't just avoid coercion in JavaScript for its corner cases. No language is free from various kinds of corner cases. We just need to be aware of those corner cases and learn how to work around them. Here are some of the corner cases we need to be aware of -&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="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 0  (!!!)&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;   &lt;/span&gt;&lt;span class="se"&gt;\t\n&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 0  (!!!)&lt;/span&gt;
&lt;span class="nc"&gt;Number&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="c1"&gt;// 0  (!!!) should be NaN!&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 0 &lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="nc"&gt;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="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="c1"&gt;// NaN&lt;/span&gt;


&lt;span class="nc"&gt;Number&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="c1"&gt;// 0  -&amp;gt; !!!&lt;/span&gt;
&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// 0  -&amp;gt; !!!&lt;/span&gt;

&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;  &lt;span class="c1"&gt;// NaN&lt;/span&gt;


&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// "0"   -&amp;gt; what! should be "-0"!&lt;/span&gt;
&lt;span class="nc"&gt;String&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="c1"&gt;// "null"  -&amp;gt; good!&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// "undefined"  -&amp;gt; good!&lt;/span&gt;
&lt;span class="nc"&gt;String&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="c1"&gt;// ""  -&amp;gt; !!!&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;  &lt;span class="c1"&gt;// ""  -&amp;gt; !!!&lt;/span&gt;

&lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Boolean&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="c1"&gt;// true  -&amp;gt; what!!!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One of the root causes of corner cases occurring in coercion in JS is the conversion of "" to 0. We have already seen what kind of problems it can cause. Many coersions would just be gone if an empty string wouldn't turn into 0 in number conversion; if it would be NaN. Whatever, we can't do anything about it now because of backward compatibility.&lt;/p&gt;

&lt;p&gt;Another important corner case is true becoming 1, and false becoming 0 in terms of converting to number operation. &lt;code&gt;Number(true)&lt;/code&gt; -&amp;gt; 1 &lt;code&gt;Number(false)&lt;/code&gt; -&amp;gt; 0&lt;/p&gt;

&lt;p&gt;Though it is an intuitive thing to us. Let's see how it creates a problem. &lt;code&gt;1 &amp;lt; 2&lt;/code&gt; -&amp;gt; true; Okay. &lt;code&gt;2 &amp;lt; 3&lt;/code&gt; -&amp;gt; true; It's also okay. &lt;code&gt;1 &amp;lt; 2 &amp;lt; 3&lt;/code&gt; -&amp;gt; true. Okay. You are thinking - it's exactly working as it should be.&lt;/p&gt;

&lt;p&gt;But that is not the case; what is happening under the hood is it first returns &lt;code&gt;true&lt;/code&gt; for the first part. Then, true converts into 1, since we are performing a numeric operation. Then, &lt;code&gt;1 &amp;lt; 3&lt;/code&gt; obviously returns true.&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see how this can create a problem.&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="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;// false --&amp;gt; !!!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see, which is actually happening.&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="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&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="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since 1 is not greater than 3, it returns false. Now, we understand how converting &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; to 1 and 0 creates problems. It would be solved if it were just NaN.&lt;/p&gt;

&lt;p&gt;Some of us hate coercion because of it's weird corner cases. But, we can't ignore coercion. We use coercion all the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Use Cases of Coercion
&lt;/h2&gt;

&lt;p&gt;Coercion is a weird topic in JavaScript. But, we can't avoid it either. It turns out, we all deal with coercion all the time! Let's see some examples.&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;numPeople&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`There are &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;numPeople&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; people out there.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// There are 19 people out there.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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;firstPart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;There are &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;numPeople&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&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;secondPart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; people out there.&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;firstPart&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;numPeople&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;secondPart&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// There are 19 people out there.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It turns out that if we use the &lt;code&gt;+&lt;/code&gt; operator and any of the values is a string, it converts the entire expression into a string. This is known as &lt;strong&gt;Operator Overloading&lt;/strong&gt;. Our number is implicitly converting into a string. Some may want to make it explicit:&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;numPeople&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`There are &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;numPeople&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; people out there.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// There are 19 people out there.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&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;numPeople&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`There are &lt;/span&gt;&lt;span class="p"&gt;${[&lt;/span&gt;&lt;span class="nx"&gt;numPeople&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; people out there.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// There are 19 people out there.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&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;numPeople&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`There are &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;numPeople&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s2"&gt; people out there.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// There are 19 people out there.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&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;numPeople&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`There are &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numPeople&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; people out there.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// There are 19 people out there.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Writing explicit code is not a good decision always.&lt;/p&gt;

&lt;p&gt;Some may ask, what about converting a string to a number? We do that too. What about form data? We all need to work with form data as JavaScript developer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addOnePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numPeople&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;numPeople&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="nf"&gt;addOnePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;peopleInputElement&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="c1"&gt;// get people number from form&lt;/span&gt;

&lt;span class="c1"&gt;// "191" - WHAT!!!!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, again, we need to convert it into a number first! There are two ways. First one -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addOnePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numPeople&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;numPeople&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="nf"&gt;addOnePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;peopleInputElement&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="c1"&gt;// 20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or we can use the &lt;code&gt;Number&lt;/code&gt; function, which is the more preferred way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addOnePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numPeople&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;numPeople&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="nf"&gt;addOnePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;peopleInputElement&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="c1"&gt;// 20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How about reducing the input value?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ignoreOnePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numPeople&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;numPeople&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="nf"&gt;ignoreOnePeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;peopleInputElement&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="c1"&gt;// 18&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;numPeople&lt;/code&gt; is a string and we are trying to perform an operation where a number is needed, the &lt;code&gt;ToPrimitive&lt;/code&gt; process is triggered, and &lt;code&gt;numPeople&lt;/code&gt; converts into a number automatically. That's why we don't need to explicitly mention that. We even need to check truthy and falsy values all the time.&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;peopleInputElement&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="nx"&gt;numPeople&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;peopleInputElement&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, if we aren't aware of the corner case of Boolean, we may face a bug here. What if the input element contains a bunch of white spaces? That won't be a falsy value!&lt;/p&gt;

&lt;h2&gt;
  
  
  Boxing
&lt;/h2&gt;

&lt;p&gt;We have seen that non-primitive values don't have methods in them. If that's the case, how can we access methods in a string like &lt;code&gt;somestring.length&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;It's called &lt;strong&gt;Boxing&lt;/strong&gt;. It's a form of implicit coercion, although it doesn't occur in the same process as abstract operations do.&lt;/p&gt;

&lt;p&gt;It's like when JavaScript sees that you are trying to run a method on a non-primitive value, JavaScript does you a favor. It thinks, "Okay, this person is trying to run an operation as if it's a primitive value (object); let's turn it into that and make their life easier!" And then, it converts it into the corresponding object representation of the string.&lt;/p&gt;

&lt;p&gt;Maybe from this, the falsy notion comes: In JavaScript, everything is an object. No, just because it is converting into an object-like structure under the hood doesn't mean it's an object. Things can behave as similar, but that doesn't make them similar.&lt;/p&gt;

&lt;p&gt;Two things are pretty different.&lt;/p&gt;

&lt;p&gt;A significant part of my motivation to delve deeply into JavaScript was influenced by &lt;a href="https://github.com/getify"&gt;Kyle Simpson&lt;/a&gt;. I conclude here by sharing some of his philosophy on "coercion."&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You don't deal with corner cases by avoiding coercions.&lt;/li&gt;
&lt;li&gt;You have to adopt a coding style that makes value types plain and obvious.&lt;/li&gt;
&lt;li&gt;A quality JS program embraces coercions, making sure the types involved in every operation are clear. Thus, corner cases are safely managed.&lt;/li&gt;
&lt;li&gt;JavaScript's dynamic typing is not a weakness, it's one of it's strong qualities.&lt;/li&gt;
&lt;li&gt;Implicit doesn't mean magic. It means abstraction. So, that it can hide unnecessary details, re-focusing the reader and increasing clarity.

&lt;ul&gt;
&lt;li&gt;We can simiply depends on implicit type coercion rather than always describing type convertion explicitely every time, which will decrease readability.&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;It's "Useful", when the reader is focused on what's important. It's "Dangerous", when the reader can't tell what will happen. It's "Better", when the reader understands the code.&lt;/p&gt;

&lt;p&gt;It's "Irresponsible" to knowingly avoid usage of a feature that can improve code readability.&lt;/p&gt;

&lt;p&gt;And it's always better to learn how things work under the hood!&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you enjoyed reading this, you can connect with me on &lt;a href="https://thefaisal.dev/twitter"&gt;&lt;strong&gt;Twitter&lt;/strong&gt;&lt;/a&gt; or check out my &lt;a href="https://thefaisal.dev/"&gt;&lt;strong&gt;other articles&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures"&gt;JavaScript Data Structures - MDN&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf"&gt;valueOf - MDN&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.prototype.valueof"&gt;valueOf in JavaScript - ECMA&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toprimitive"&gt;Abstract Operations: To Primitive - ECMA&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/getify/You-Dont-Know-JS"&gt;You Don't Know JS by Kyle Simpson&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>7 Reasons Why You Need to Master the Fundamentals (A Visual Guide)</title>
      <dc:creator>Faisal </dc:creator>
      <pubDate>Wed, 22 Feb 2023 02:29:12 +0000</pubDate>
      <link>https://forem.com/thefaisal/7-top-reasons-why-you-need-to-master-the-fundamentals-a-visual-guide-apo</link>
      <guid>https://forem.com/thefaisal/7-top-reasons-why-you-need-to-master-the-fundamentals-a-visual-guide-apo</guid>
      <description>&lt;p&gt;Fundamentals are boring.&lt;/p&gt;

&lt;p&gt;Admit it; we've all been there before: wanting to skip the dry and boring fundamentals and jump right into the exciting stuff. I know I have. But trust me when I say this: taking time to learn the basics can make all the difference in the long run. I had to learn this the hard way. But you’ll not have to make the same mistake I did.&lt;/p&gt;

&lt;p&gt;Here are my 7 takeaways on why you should be patient enough to master the fundamentals first.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Builds a strong foundation
&lt;/h2&gt;

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

&lt;p&gt;Fundamentals are the building blocks of the more advanced concepts you'll learn in the future. They help to establish a strong foundation upon which you can build more advanced knowledge or expertise. Your understanding of the subject will be solid and complete with a strong foundation.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Enables innovation
&lt;/h2&gt;

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

&lt;p&gt;Creating anything truly impressive or innovative can only be easy with a solid understanding of the basics. Because innovation requires a mental understanding of certain underlying frameworks, this understanding will allow you to devise innovative solutions that can change the game. Innovation often comes from building on existing knowledge and improving it. It is only possible if you have a strong grasp of the basics.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Effortless learning
&lt;/h2&gt;

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

&lt;p&gt;Once you master the fundamentals, it becomes easier to learn more advanced concepts. It may take time to build the foundation. But when you build the foundation, you can easily jump from one advanced topic to another.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Makes you efficient
&lt;/h2&gt;

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

&lt;p&gt;You can increase your efficiency and productivity in the long run by mastering the basics. If you skip the basics and rush to advanced concepts, you may struggle to understand the material. It will slow down your learning process. Also, with a strong foundation, you will be able to finish tasks fast and are less likely to make mistakes.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Makes you a better problem-solver
&lt;/h2&gt;

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

&lt;p&gt;Fundamentals help you develop critical thinking and problem-solving skills. You can better analyze and break big problems into small pieces and develop more effective solutions.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Builds confidence
&lt;/h2&gt;

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

&lt;p&gt;Mastering the underlying principles lets you see how new and complex information fits together. It builds confidence and helps you feel more comfortable with the material. You can approach more advanced concepts easily and confidently with a solid foundation. Also, you will know what is happening and why. It also helps to reduce your imposter syndrome.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Provides versatility
&lt;/h2&gt;

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

&lt;p&gt;Understanding the fundamentals of a subject can make you more versatile and adaptable. You can apply that knowledge in various situations by deeply understanding the basics. Also, you can connect knowledge among multiple disciplines if you master the fundamentals.&lt;/p&gt;

&lt;p&gt;So, Even though it may be tempting to skip over the basics, stick with it and give the fundamentals the attention they deserve.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recap
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;Fundamentals are important because it-&lt;/li&gt;
&lt;li&gt;builds a strong foundation&lt;/li&gt;
&lt;li&gt;enables innovation&lt;/li&gt;
&lt;li&gt;makes learning effortless&lt;/li&gt;
&lt;li&gt;increases efficiency and productivity&lt;/li&gt;
&lt;li&gt;develops problem-solving skills&lt;/li&gt;
&lt;li&gt;builds confidence and reduces imposter syndrome&lt;/li&gt;
&lt;li&gt;provides versatility and adaptability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it! If you enjoyed reading this, you can connect with me on Twitter or check out my other articles. Additionally, you can subscribe to my non-tech newsletter, where I share my thoughts on life, productivity, and more, as well as the best content I’ve come across each month.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>productivity</category>
      <category>career</category>
      <category>motivation</category>
    </item>
    <item>
      <title>A Deep Dive into Node.js: Understanding its history, threading, and event-driven architecture</title>
      <dc:creator>Faisal </dc:creator>
      <pubDate>Sat, 18 Feb 2023 04:58:11 +0000</pubDate>
      <link>https://forem.com/thefaisal/a-deep-dive-into-nodejs-understanding-its-history-threading-and-event-driven-architecture-23j1</link>
      <guid>https://forem.com/thefaisal/a-deep-dive-into-nodejs-understanding-its-history-threading-and-event-driven-architecture-23j1</guid>
      <description>&lt;h2&gt;
  
  
  What is Node.js?
&lt;/h2&gt;

&lt;p&gt;According to Node’s official documentation, Node.js is a JavaScript runtime built on Google’s open-source V8 JavaScript engine. Node.js can execute JavaScript code on the server side, making it possible to create fast, scalable, high-performance network applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  History of Node.js
&lt;/h2&gt;

&lt;p&gt;Node.js was &lt;a href="https://www.youtube.com/watch?v=ztspvPYybIY&amp;amp;t=372s&amp;amp;pp=ygUhcnlhbiBkYWhsIGZpcnN0IG5vZGUgcHJlc2VudGF0aW9u" rel="noopener noreferrer"&gt;introduced&lt;/a&gt; in 2009 by &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Ryan_Dahl" rel="noopener noreferrer"&gt;Ryan Dahl&lt;/a&gt;&lt;/strong&gt;. Despite initial skepticism, the concept gained popularity in 2011 when Dahl presented Node.js at a PHP meetup in San Fransisco on 22 Feb 2011. The &lt;a href="https://www.youtube.com/watch?v=jo_B4LTHi3I&amp;amp;t=189s&amp;amp;pp=ygUhcnlhbiBkYWhsIGZpcnN0IG5vZGUgcHJlc2VudGF0aW9u" rel="noopener noreferrer"&gt;video&lt;/a&gt; went viral, drawing attention to using JavaScript on the server side. In that &lt;a href="https://www.youtube.com/watch?v=jo_B4LTHi3I&amp;amp;t=189s&amp;amp;pp=ygUhcnlhbiBkYWhsIGZpcnN0IG5vZGUgcHJlc2VudGF0aW9u" rel="noopener noreferrer"&gt;video&lt;/a&gt;, Ryan Dahl explained Node.js in front of a bunch of programmers. He explained how he developed a new JavaScript runtime with the help of the v8 engine of Google Chrome and how we can use JavaScript on the server side. Before that, JavaScript was used only on the client and browser sides. After listening to Ryan’s explanation, most programmers made fun of him because they couldn’t accept JavaScript as a server-side language. At that time, some hot server-side languages like Ruby on Rails existed. But Ryan Dahl didn’t stop. Dahl saw the potential for using JavaScript on the server side and developing and improving Node.js as a solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reasons Behind the Popularity of Node.js
&lt;/h3&gt;

&lt;p&gt;There was some critical incident that helped the Node gain its popularity.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;strong&gt;2010&lt;/strong&gt;, Isaac Z. Schlueter created NPM, short for Node Package Manager. At the time, Node.js was a relatively new platform and lacked a standard package manager like those used in other programming languages. It made it easier for developers to share their code with other developers and reuse existing packages in their projects.&lt;/li&gt;
&lt;li&gt;In &lt;strong&gt;2007&lt;/strong&gt;, Dwight Merriman and Eliot Horowitz created MongoDB. It uses a JSON-style binary data structure to store data which is easy to use with JavaScript. As expected, developer communities did not accept a NoSQL database like MongoDB because the SQL relational database was popular then. As social media companies like Twitter and Facebook started to publish APIs in JSON format, JSON-style data structures increased as developers found it more challenging to work with relational databases and JSON-format data. As a result, solutions like MongoDB and Node.js became more widely adopted.&lt;/li&gt;
&lt;li&gt;Node.js has made it easier for developers to use JavaScript in both the front-end and back-end for their web applications. This capability has been a significant factor in developers’ popularity of Node.js. By using the same language on both the client and server sides, Node.js eliminates the need to switch between multiple programming languages, leading to more streamlined and efficient development. Furthermore, since JavaScript is a widely-used language, many developers already have experience with it, making the transition to using Node.js for server-side development a natural choice. These factors have contributed to Node.js becoming a go-to solution for building fast, scalable, and highly performant network applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Architecture of Node.js and Understanding the V8 Engine
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj20nuti970fibq7fcisj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj20nuti970fibq7fcisj.jpg" alt="Node.js Architecture" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s learn about the node architectures regarding the Node’s dependencies.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Node.js has several dependencies to work/ function properly. The most important ones are the v8 engine and the libuv. We saw earlier that Node.js was built on Chrome’s v8 engine. First, we must know the role of the v8 engine here.&lt;/p&gt;

&lt;p&gt;Many of us write JavaScript for the browser. When we write code, machines don’t understand our code directly. The machine must convert it into machine code to understand what we command. There are engines in the browser to understand our code as machine code. The different browsers have different engines to understand/convert JavaScript code into machine code. Here are some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google Chrome: V8&lt;/li&gt;
&lt;li&gt;Mozilla Firefox: Spider Monkey&lt;/li&gt;
&lt;li&gt;Apple Safari: JavaScriptCore&lt;/li&gt;
&lt;li&gt;Microsoft Edge: Chakra&lt;/li&gt;
&lt;li&gt;Opera: Blink (which is based on the same engine as Google Chrome, V8)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most powerful one is Chrome’s v8 engine.&lt;/p&gt;

&lt;p&gt;On the server side, there has to be something that converts JavaScript code to machine code. For this, Ryan Dahl used a super-speed V8 engine in Node.js. Now, by this, Node.js can convert JavaScript code into machine code. That’s the use case of the V8 engine in Node.&lt;/p&gt;

&lt;h2&gt;
  
  
  libuv Library
&lt;/h2&gt;

&lt;p&gt;V8 engine is not alone enough to run a server-side framework like Node.js. Here comes another critical library called “libuv.” It’s an open-source library that strongly focuses on asynchronous IO(Input/output).&lt;/p&gt;

&lt;p&gt;By libuv, Node.js can access the underlying computer operating system, file system, stuff related to networking, and more.&lt;/p&gt;

&lt;p&gt;“libuv” implements &lt;strong&gt;two&lt;/strong&gt; critical features of Node.js —&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Event Loop&lt;/strong&gt;, which handles easy tasks like a callback or network io&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread Pool&lt;/strong&gt;, to handle heavy work like accessing files or file compression.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I’ll discuss these two featured in detail later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is Node.js Entirely Written in JavaScript?
&lt;/h2&gt;

&lt;p&gt;We intuitively think there is only JavaScript code in Node.js behind the scenes, as Node.js is a JavaScript runtime. But that’s not the case. As I said, libuv is an essential part of Node, and libuv is entirely written in C++. On the other hand, V8 is also written in JS &amp;amp; C++. So, Node is not only written in JavaScript but also C++. But there are some abstraction layers so we can access all functions and everything in the Node by pure JavaScript functions. We don’t have to mess up with C++ or any underlying code. For example- for file reading, we write a pure js function to read from the file system, which functionality is written in libuv in C++. Pretty interesting.&lt;/p&gt;

&lt;p&gt;An important thing to note is that Node.js depends not only on the V8 engine and libuv. There are other libraries as well, like — HTTP parser (for parsing HTTP), c-ares (for handling DNS request stuff), OpenSSL (for cryptography), zlib (for file compression), etc.&lt;/p&gt;

&lt;p&gt;Now let’s see what thread and thread pool are.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thread &amp;amp; Thread Pool
&lt;/h2&gt;

&lt;p&gt;A node process is initiated on our computer whenever we use Node.js. This process represents a program that is currently in execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbco86cndea0djfej05hs.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbco86cndea0djfej05hs.jpg" alt="Node.js Process" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Within that process, &lt;strong&gt;the Node runs in a single thread&lt;/strong&gt;. A thread is like a mini-program within the bigger program, a sequence of instructions that can perform multiple tasks simultaneously. It’s like a box where our code is executed.&lt;/p&gt;

&lt;p&gt;It’s essential to understand the concepts of &lt;strong&gt;multithreading&lt;/strong&gt; and &lt;strong&gt;single threading&lt;/strong&gt; clearly. Multithreading enables the execution of multiple operations simultaneously, where several tasks can run in the background at the same time. On the other hand, single threading only allows one operation to be performed at a time. In other words, it operates in a linear sequence and cannot execute multiple operations simultaneously.&lt;/p&gt;

&lt;p&gt;It’s important to understand that Node runs on a single thread, no matter we have how many users. This means that &lt;strong&gt;if the single thread gets blocked, the entire application can be affected&lt;/strong&gt;. So, we need to be extra mindful of actions that could potentially block the thread.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s see what happens when the Node runs in a single thread.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When a Node application is run, the program goes through several stages. Node.js &lt;strong&gt;initializes&lt;/strong&gt; the program, &lt;strong&gt;executes all the top-level code&lt;/strong&gt;, &lt;strong&gt;requires necessary modules&lt;/strong&gt;, and then &lt;strong&gt;event callbacks are registered.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once all these steps are completed, the &lt;strong&gt;event loop&lt;/strong&gt; starts running. Event loop where the majority of the work of the application gets done. It’s the heart of the application. But, some tasks can be resource-intensive and heavy. They can potentially block the single thread if executed within the event loop. Handling these heavy tasks outside the event loop is important to avoid this.&lt;/p&gt;

&lt;p&gt;Here, the &lt;strong&gt;thread pool&lt;/strong&gt; comes to the rescue, which the libuv library provides. The event loop offloads those heavy tasks to the thread pool. It happens behind the scene. We don’t have to worry about which tasks will go to the thread pool and which will not.&lt;/p&gt;

&lt;p&gt;The thread pool has four additional threads separate from the single main thread. We can configure a thread pool of up to 128 threads. But, usually, we don’t need to. Four threads are enough.&lt;/p&gt;

&lt;p&gt;Here are some task examples that are offloaded to the thread pool from Event Loop -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Tasks that use file system API&lt;/strong&gt;: When we perform operations like reading or writing files, these tasks can take a significant amount of time. They can block a single thread and cause performance issues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cryptography-related task:&lt;/strong&gt; Cryptography involves complex mathematical calculations. And performing these operations in the Event Loop can slow down our application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compression tasks:&lt;/strong&gt; When we need to compress large data like images or videos, these tasks can take a lot of time and can also slow down the Event Loop. Therefore, these tasks are offloaded to the thread pool to prevent this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DNS lookup:&lt;/strong&gt; When we need to resolve a domain name into an IP address, it’s done through a DNS lookup process. This process can also take time and block the Event Loop, which may cause performance issues. Therefore, to prevent this, these tasks are offloaded to the thread pool, where they can run concurrently without blocking the main thread.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Event Loop and Its Implementation in Node.js
&lt;/h2&gt;

&lt;p&gt;Now, we are in the Node.js process. This process contains a thread where the event loop runs. The Event Loop is an essential component of the Node.js process. It is responsible for executing all of the code within callback functions that make up a Node.js application.&lt;/p&gt;

&lt;p&gt;As we learned earlier, some tasks may also be offloaded to the thread pool for efficient processing. But, the event loop is the central mechanism of the Node.js runtime. Node.js is based on a callback-driven architecture. It means that functions are called as soon as a certain event has finished or been emitted. This happens because of the event-driven architecture of Node.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event-Driven Architecture
&lt;/h2&gt;

&lt;p&gt;Event-driven architecture is a system where events are emitted, picked up, and processed by the Event Loop. Then the Event Loop executes the associated callback functions to that event.&lt;/p&gt;

&lt;p&gt;For example, events such as a new HTTP request, expiration of a timer, or a finished file reading or writing into a file operation will emit events, which the Event Loop will then pick up. It then processes those events by executing the associated callback functions. The Event Loop observes events and performs the necessary orchestration behind the scenes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s take a closer look at how the event loop actually operates behind the scene:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The event loop starts running as soon as the Node.js application starts. The Event Loop has several phases. Each phase has its own callback queue. Some might say the Event Loop has only one callback or event queue. But it has multiple phases, each with its own callback queue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ik7wra9h4hwio8jkpto.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ik7wra9h4hwio8jkpto.jpg" alt="Four Phases of Event Loop" width="800" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The four most important phases of the Event Loop are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Handling expired timer callbacks (setTimeout()):&lt;/strong&gt; The first phase involves executing callbacks from expired timers. If a timer expires during any of the other phases, its associated callback function will be executed as soon as the current phase finishes, and the event loop returns to its first phase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;I/O polling and execution of I/O callbacks:&lt;/strong&gt; Node IO means networking or file system stuff like file reading, writing into new files, etc., and Polling means — looking for new IO events that are ready to be processed and place them in the callback queue. This phase is where 99% of the application code is executed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Executing SetImmediate callbacks:&lt;/strong&gt; This is a special type of timer that allows code to be executed immediately after the execution of the I/O callbacks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Executing close callbacks:&lt;/strong&gt; The Event Loop processes all close events in this phase. This includes events such as closing a web server or a web socket.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In addition to these four phases, two other queues are important to be aware of:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The process.nextTick() queue:&lt;/strong&gt; This queue executes its callbacks immediately after the current phase finishes. It is similar to the SetImmediate callbacks. The only difference is that SetImmediate callbacks only execute immediately right after the execution of the I/O callbacks, but process.nextTick() executes right after any of the phases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The microtask queue for resolving promises:&lt;/strong&gt; This queue executes its callbacks immediately after the current phase finishes, just like the process.nextTick() queue. If there are any callbacks in either of these two queues, they will be executed immediately after the current phase is completed rather than waiting for the entire event loop to finish its four phases. For example, if a promise resolves and returns data from an API call while the callback of an expired timer is running, its callback will be executed immediately after the timer’s callback has finished.&lt;/p&gt;

&lt;p&gt;After each of the first four phases, the event loop checks if there are any callbacks in these two queues. If there are, they will be executed right away. This completes one tick of the event loop. A tick is defined as one cycle of the loop. After one cycle, the Node.js runtime checks if there are any pending timers or I/O tasks, or any callback of any phases. If there are, the loop runs again. Otherwise, the application exits.&lt;/p&gt;

&lt;p&gt;Asynchronous programming is made possible in Node.js due to the Event Loop, which makes the Event Loop the most important feature of Node.js. It allows Node.js to run in a single thread, making it lightweight and fast but also presenting some potential risks. Since Node.js is a single-threaded program, it’s possible to block our application accidentally. So, it’s important to take extra care while writing code to avoid accidentally blocking the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps to Avoid Blocking the Event Loop
&lt;/h2&gt;

&lt;p&gt;Here are some tips to avoid blocking the code in Node.js:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Avoid using synchronous versions of functions; write any necessary synchronous code outside of any callback functions. This way, the code will be executed before the event loop starts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid complex calculations such as nested loops in Node.js, as they can cause the application to become blocked.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Be cautious when dealing with large JSON objects, as parsing or stringifying them can become time-consuming.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid using complex regular expressions, as they require a lot of processing power that can be resource-intensive and slow down the event loop. If we use a complex regular expression, it can take significant time to execute. That’s why it is also advisable to break down large regular expressions into smaller, simpler ones that can be executed more efficiently. This will ensure that the event loop continues to run smoothly and that the application remains responsive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do not perform any CPU-intensive tasks within the event loop. It can cause the application to become blocked. Here are some CPU-intensive tasks example-&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Heavy computation, such as mathematical calculations and scientific simulations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Image and video processing, such as resizing, cropping, and filtering images and videos&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cryptographic operations, such as encryption and decryption of data&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Data compression and decompression&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It’s important to be careful if we want to do these tasks in Node.js. We will have to take extra steps to minimize their impact on the event loop. This can be done by manually offloading these tasks to a separate process or thread or using an external library optimized for performing these types of operations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In addition, it’s important to structure the application’s code in a way that doesn’t put pressure on the event loop, such as by avoiding complex algorithms, minimizing blocking operations, and using asynchronous programming techniques wherever possible. This will help to ensure that our Node.js application remains fast, efficient, and responsive even under heavy load.&lt;/p&gt;

&lt;p&gt;That’s it for today!&lt;/p&gt;

&lt;p&gt;Congratulations on finishing this article! You now have a deeper understanding of Node.js. Whether a beginner or an experienced developer, this information is valuable in helping you build efficient, scalable, and high-performance applications.&lt;/p&gt;

&lt;p&gt;If you enjoyed reading this, you can connect with me on &lt;a href="https://thefaisal.dev/twitter" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or check out my &lt;a href="https://thefaisal.dev/" rel="noopener noreferrer"&gt;other articles&lt;/a&gt;. Additionally, you can subscribe to my &lt;a href="https://thefaisal.dev/talks" rel="noopener noreferrer"&gt;non-tech newsletter&lt;/a&gt;, where I share my thoughts on life, productivity, and more, as well as the best content I’ve come across each month.&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>How the Web Works: Understanding the Architecture of the Web</title>
      <dc:creator>Faisal </dc:creator>
      <pubDate>Wed, 15 Feb 2023 03:18:08 +0000</pubDate>
      <link>https://forem.com/thefaisal/how-the-web-works-understanding-the-architecture-of-the-web-1d7d</link>
      <guid>https://forem.com/thefaisal/how-the-web-works-understanding-the-architecture-of-the-web-1d7d</guid>
      <description>&lt;p&gt;It’s easy to take for granted the intricacies of the web as we seamlessly browse through various websites daily. We enter a URL into our browser, and within seconds, a whole website appears. But have you ever stopped to wonder about the technology and processes that make this possible? This article delves into the various components and technologies that make the seemingly effortless experience of accessing the web a reality.&lt;/p&gt;

&lt;p&gt;Also, You don’t need to know how the web works to write code. But it will help you to understand the whole system.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when we try to access a website
&lt;/h2&gt;

&lt;p&gt;We know our website’s code is not stored on our computer. We need to get the code into our computer or browser from where it is kept to visit the website. How does this happen?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwm2w3n2bnxmuwafw9ra9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwm2w3n2bnxmuwafw9ra9.jpg" alt="request-response model" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The process is quite simple. When we enter a URL into our browser, the browser sends a request for the necessary data to the server. The server then responds by sending back the code and data for the website. The browser interprets the codes and displays the website for us to view. This process is known as the request-response model or client-server architecture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now let’s look at what the **client **and **server **are.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;**Clients **are those devices connected to the internet, like our phones or computers connected to the mobile network or wi-fi. The client is where all the user interaction happens. In the web context, a client is typically a web-accessing software like a browser, such as Chrome, Firefox, or Safari, that requests web pages and other resources from a server. The client (web browser) receives the code for the website and then renders it for the user to view.&lt;/p&gt;

&lt;p&gt;Though we access the website from the browser, we can treat the whole device as a client of the client-server architecture.&lt;/p&gt;

&lt;p&gt;On the other end of the spectrum are &lt;strong&gt;Servers&lt;/strong&gt;, specialized computers designed to store and manage data, websites, and web applications. These servers are called such because they serve up code or data in response to requests made by clients. A server waits for requests to come in from clients, processes the request, and then sends back the requested information. There are various types of servers, such as web servers, file servers, and database servers, each with its specific function. In this article, we will primarily focus on web servers.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is URL, and how does it get resolved
&lt;/h2&gt;

&lt;p&gt;A URL (Uniform Resource Locator) is a string of characters that specifies the location of a resource on the internet. URLs are used to identify and locate web pages, images, videos, and other resources on the World Wide Web. URLs typically contain several parts, including a protocol, domain name, and path.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx276tonabgafwwwgti1f.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx276tonabgafwwwgti1f.jpg" alt="parts of url" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The protocol is the method used to transfer data over the internet. The most common protocol used for the web is HTTP (Hypertext Transfer Protocol) or HTTPS (HTTP Secure), a more secure version of HTTP that encrypts data for added security.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The domain name is the unique name that identifies a website or web server on the internet. To access a website, the name is typed into the browser’s address bar. For example, “github.com” is the domain name of Github’s website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The path indicates the location of the resource within the server. This URL part usually includes the file name or folder containing the resource, such as “about” or “contact.” For example, “github.com/about” is the path for the about page of the website “github.com.”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optionally, the URL may also include a query string, a set of characters added to the end of the URL that contains additional information or parameters for the resource.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3s5d2s28dqs3ix6iv93.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq3s5d2s28dqs3ix6iv93.jpg" alt="DNS Lookup" width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s interesting to note that the domain name we enter into the browser is not the actual physical address of the website. Every website and device connected to the internet has a unique IP address, a numerical label that is often difficult to remember. To make it easier to remember, we use domain names. When an URL is entered, the browser first sends a request to the DNS (Domain Name System) server, which matches the domain name to the corresponding IP address of the website. This is called DNS Lookup. This process is facilitated by your internet service provider and returns the IP address, including the port number of the server being accessed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Establishing protocols
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pvhbl9cmgsrbq19vlzx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pvhbl9cmgsrbq19vlzx.jpg" alt="protocols" width="800" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we enter a web address and get the actual IP address of the website in the browser, a connection known as a TCP/IP socket is established between the browser and the server. This connection remains active while files are being transferred from the server to the client.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbl7efpbfdg7cu6z95wf3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbl7efpbfdg7cu6z95wf3.jpg" alt="TCP/IP" width="800" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TCP (Transmission Control Protocol) and IP (Internet Protocol) are two of the main protocols that make up the internet protocol (IP) suite. Together, they provide the foundation for communication on the internet.&lt;/p&gt;

&lt;p&gt;TCP (Transmission Control Protocol) is one of the core protocols of the internet protocol suite (TCP/IP), which is used to establish and maintain connections between devices on a network. The primary function of TCP is to ensure the reliable delivery of data between devices.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fog2byfme3onfstpp4opj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fog2byfme3onfstpp4opj.jpg" alt="packets" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TCP breaks down data into small chunks called packets before they are sent over the network. Each packet is labelled with a TCP header, which includes the source and destination port numbers, and an IP header which consists of the source and destination IP addresses, to identify it. Each packet also contains a sequence number that allows the receiving device to reassemble the packets in the correct order. If a packet is lost or corrupted during transmission, TCP will automatically retransmit the packet to ensure that all packets are received correctly.&lt;/p&gt;

&lt;p&gt;This protocol also provides flow control and congestion control. Flow control ensures that the sender does not overwhelm the receiver by sending too much data at once, and congestion control ensures that the network does not become overloaded by too much traffic.&lt;/p&gt;

&lt;p&gt;It is a connection-oriented protocol that establishes a virtual connection between the sender and receiver before exchanging any data. Once a connection is established, both parties can exchange data in a reliable and orderly manner.&lt;/p&gt;

&lt;p&gt;TCP is widely used in many applications, including web browsing, email, file transfer, and online gaming. It is a reliable and efficient protocol that ensures data is delivered correctly and in the right order, making it an essential part of the internet infrastructure.&lt;/p&gt;

&lt;p&gt;On the other side, IP addresses and routes packets between devices on a network. It assigns a unique IP address to each device connected to the internet. When data is sent from one device to another, the IP protocol determines the destination address and the best route for the data to take. Its job is to send and route all the packets through the internet.&lt;/p&gt;

&lt;p&gt;TCP and IP work together to provide reliable, efficient communication on the internet. IP addresses and routes data packets, while TCP ensures that the data is delivered correctly and in the proper order. These are communication protocols or the internet’s fundamentals control system that defines and sets the rules for how data travels across the web. Communication Protocol sets the rules for how two or more parties communicate within them.&lt;/p&gt;

&lt;p&gt;Once the connection is established, the communication process begins with an HTTP request sent from the browser. HTTP, or Hypertext Transfer Protocol, is the standard protocol for transmitting data over the internet and is the backbone of the World Wide Web. It enables the browser to send requests to a web server and receive the server’s response.&lt;/p&gt;

&lt;p&gt;HTTP is based on a request-response model we discussed before, in which a client (such as a web browser) sends a request to a server, and the server sends a response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsen7p74b7qlsnv00pl12.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsen7p74b7qlsnv00pl12.jpg" alt="request-response header" width="800" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTTP Request has a few parts, which include the start line(HTTP method + request target + HTTP version), HTTP request header, and request Body.&lt;/p&gt;

&lt;p&gt;The most common HTTP methods are GET, which requests a resource from the server, and POST, which sends data to the server to be processed. There are also PUT and Patch methods that are used to modify data.&lt;/p&gt;

&lt;p&gt;HTTP sits on top of the TCP/IP protocol stack. It’s usually used with the SSL/TLS (Secure Sockets Layer/Transport Layer Security) protocols to provide secure and encrypted communication over the internet.&lt;/p&gt;

&lt;p&gt;When the server receives the request, it processes it and sends back an HTTP Response. &lt;strong&gt;The HTTP Response is composed of several parts:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The start line includes the HTTP version, status code, and message. The HTTP version indicates which version of the HTTP protocol is being used. The status code is a three-digit numerical code that indicates the outcome of the request. The status message is a brief text description of the status code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The HTTP response header contains additional information about the response, such as the type of content in the response body, the date and time the response was sent, and the server’s name. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The response body contains the actual data or HTML file that was requested.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The backend developer is responsible for specifying the data included in the response header. This data can include things like the content type, date and time, and server name. In the response body, we get the data or HTML file requested in the original request. If the server cannot locate the requested page, it will send an HTTP 404 error message indicating that the page could not be found.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens next after our browser receives the response?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wopaoceix2lv8vghmxb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wopaoceix2lv8vghmxb.jpg" alt="Rendering Engine Flow" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a web browser receives an HTML (Hypertext Markup Language) document from a web server, it parses the document to convert it into a visual representation that can be displayed to the user. The process of parsing an HTML document involves the following steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The browser begins by reading the HTML code from top to bottom, line by line.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;As it reads through the code, the browser looks for tags, which are used to define the structure and layout of the document. For example, the  tag indicates the start of an HTML document, and the &lt;/p&gt; and  tags indicate the beginning of the head and body sections of the document, respectively. The browser understands how to interpret the code by an HTML parsing algorithm.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As the browser encounters tags, it creates corresponding elements in the Document Object Model (DOM), a tree-like representation of the HTML document. The browser uses it to understand how the page should be rendered and displayed. Each element in the DOM corresponds to a single tag in the HTML code, and we can manipulate those elements by scripting language like JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bf2eulentsr20u8235s.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bf2eulentsr20u8235s.jpg" alt="request-response flow" width="800" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The browser scans the document for additional assets to render the page correctly. These assets can include CSS files, JavaScript files, images, and other multimedia files. When the browser encounters a reference to an asset in the HTML document, it will send an additional request to the server to retrieve that asset. The browser will repeat this process for each asset that it encounters. For example, if the HTML document links.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For a CSS file, the browser will request the server to retrieve that file. The CSS file will then be parsed and used to apply the visual styles to the elements defined in the HTML. Similarly, if there are images on the page, the browser will send a request to the server for each image and render them on the page. It’s important to note that the browser will only download and parse the assets necessary for the current viewport or the visible part of the webpage. This is called Lazy Loading. As the user scrolls down the page, the browser will download and parse additional assets as they are needed. This downloading and parsing of additional assets can impact the web page’s performance, as it can take time to retrieve and process these files. Therefore, developers need to optimize the loading of these assets to ensure that the page loads quickly and efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It’s important to understand that HTML doesn’t provide information on how a website should appear visually. It only defines the structure and tells the browser the different parts of the content, like headings, images, and paragraphs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CSS (Cascading Style Sheets) provides visual styling and presentation information for a web page. CSS allows developers to specify the font, colour, size, and position of elements on a web page, as well as other visual properties. CSS can be linked to an HTML document and will be used to apply the styles to the elements defined in the HTML.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The browser also uses JavaScript to add dynamic functionality to web pages, like form validation and interactive elements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the parsing process is complete, the browser renders the visual representation of the HTML document, which the user can interact with and view in the browser window.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The parsing process is complex, but it is done quickly and efficiently by modern web browsers, allowing users to interact seamlessly with web pages. After parsing everything, the connection gets closed.&lt;/p&gt;

&lt;p&gt;This is it for now! However, many other complex processes and technologies make the internet function. This article should give you an overview of the essential parts of the web, but keep in mind that there is much more to learn and explore in the field of the web.&lt;/p&gt;

&lt;p&gt;If you enjoyed reading this, feel free to connect with me on &lt;a href="https://thefaisal.dev/twitter" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or check out my &lt;a href="https://thefaisal.dev" rel="noopener noreferrer"&gt;other articles&lt;/a&gt;. Additionally, you can subscribe to my &lt;a href="https://thefaisal.dev/talks" rel="noopener noreferrer"&gt;non-tech newsletter&lt;/a&gt;, where I share my thoughts on life, productivity, and more, as well as the best content I’ve come across each month.&lt;/p&gt;

&lt;p&gt;Happy Learning.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
