<?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: Arika O</title>
    <description>The latest articles on Forem by Arika O (@arikaturika).</description>
    <link>https://forem.com/arikaturika</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%2F433146%2Fdbb84bca-03d0-4b56-9627-3ac7b3e2c857.png</url>
      <title>Forem: Arika O</title>
      <link>https://forem.com/arikaturika</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/arikaturika"/>
    <language>en</language>
    <item>
      <title>Tree shaking in Javascript - short explanation</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Sun, 31 Mar 2024 14:13:58 +0000</pubDate>
      <link>https://forem.com/arikaturika/tree-shaking-in-javascript-short-explanation-3ci4</link>
      <guid>https://forem.com/arikaturika/tree-shaking-in-javascript-short-explanation-3ci4</guid>
      <description>&lt;p&gt;&lt;code&gt;Tree shaking&lt;/code&gt; is a term commonly used with Javascript and it refers to the process of eliminating dead code when trying to optimize it. &lt;/p&gt;

&lt;p&gt;Before moving further, we should first understand what "dead code" means: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Dead code refers to code that is not being executed and has no impact on the final output. If you have &lt;code&gt;unused variables and functions&lt;/code&gt; or &lt;code&gt;lines that will never be reached&lt;/code&gt;, you most likely suffer of an obvious case of "dead code".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that we understand what dead code is, you might ask yourself, when and why do we need to eliminate it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;THE WHY&lt;/strong&gt;&lt;br&gt;
When sending Javascript over the network, the code is often compressed. What this mean is that, in reality the code is much bigger after the browser decompresses it. Then the hard work starts: after is being downloaded by the browser, JavaScript must be parsed, compiled and then finally executed. Compared to other resources (images for example), Javascript is expensive to process, so we had to come up with ways of making the code more performant. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;THE WHEN&lt;/strong&gt;&lt;br&gt;
Among other techniques, tree shaking is one way we can improve Javascript's performance by reducing bundle size. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Bundle size refers to the combined file size of all JavaScript code, libraries, and dependencies that are bundled together and served to the end-user in their browser. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Bigger bundle size means longer loading times, so the goal is smaller bundle sizes. Tree shaking can help us achieve that while the code gets compiled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;THE HOW&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We declare all of our &lt;code&gt;imports&lt;/code&gt; and &lt;code&gt;exports&lt;/code&gt; for each of our Javascript modules.&lt;/li&gt;
&lt;li&gt;The bundler (Webpack, Rollup, Parcel etc) analyzes our dependency tree during the compilation.&lt;/li&gt;
&lt;li&gt;All unused code is dropped from the final bundle.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT - take advantage of the ES6 imports and exports&lt;/strong&gt;&lt;br&gt;
If you want tree shaking to happen, you must use &lt;code&gt;exports&lt;/code&gt; and &lt;code&gt;imports&lt;/code&gt; in your modules. If you use &lt;code&gt;require&lt;/code&gt;, the bundler isn't able to reliably tell what is actually exported or imported so it can't determine what code is safe to drop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RESOURCES&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking"&gt;Tree shaking&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://webpack.js.org/guides/tree-shaking/"&gt;Tree shaking with Webpack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/angular-bundle-size"&gt;How To Analyze Angular App Bundle Sizes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;IMAGE SOURCE&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.pexels.com/@pavel-danilyuk/"&gt;Pavel Danilyuk&lt;/a&gt; on &lt;a href="https://www.pexels.com/@pavel-danilyuk/"&gt;Pexels&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Early return pattern in JavaScript</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Wed, 27 Dec 2023 21:03:52 +0000</pubDate>
      <link>https://forem.com/arikaturika/one-concept-a-day-early-return-pattern-in-javascript-3pol</link>
      <guid>https://forem.com/arikaturika/one-concept-a-day-early-return-pattern-in-javascript-3pol</guid>
      <description>&lt;p&gt;This concept might sound fancy but I bet you are already using it and maybe you didn't know it had a name. It is an approach mainly used to keep readability of methods and functions and it is not specific to Javascript. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The Early Return pattern is a coding technique where a function or method is stopped as soon as a specific condition is met and evaluates to true. Instead of proceeding with the rest of the function's logic, the method immediately returns a value or performs an action based on the condition's outcome.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's dive deeper into this definition using an example. We have a very simple function with two parameteres of type integer that returns their sum.&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;addTwoIntegers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstInteger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;firstInteger&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;secondInteger&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;Looks about right, doesn't it? But let's say that we want to throw an error if one of the arguments is not an integer.&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;addTwoIntegers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstInteger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;secondInteger&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstInteger&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;secondInteger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Both arguments need to be integers!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&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;Chrisis averted. Now another requirement gets added: both integers need to be positive.&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;addTwoIntegers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstInteger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;secondInteger&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstInteger&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;secondInteger&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstInteger&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;secondInteger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Both integers need to be positive!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Both arguments need to be integers!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&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;I believe you already notice the code becoming harder to read. Imagine that more requirements would be added to the function and we would need to implement all of them. We could of course do this using multiple if-else statements or we could go back to our defintion and try to re-write our function using the early return pattern. For this, we would write &lt;strong&gt;everything that we wouldn't want to happen in our function at the top, and everything that we want to happen at the bottom&lt;/strong&gt;. In simple terms, we will be reversing the conditions we wrote in the initial 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;addTwoIntegers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstInteger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstInteger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isInteger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;secondInteger&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Both arguments need to be integers!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firstInteger&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;secondInteger&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Both integers need to be positive!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The code in our last example is easier to read because we removed nested conditionals. Also, the expected result of our function is written at the end, so it's easy to find.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;&lt;br&gt;
In some situation, this pattern has no real advantage, so be mindful of when to use it. It is recommened to return early if we have simple conditions that can be checked at the beginning of a function or method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Refrence materials:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.c-sharpcorner.com/article/early-return-pattern-in-c-sharp/#:~:text=The%20Early%20Return%20pattern%20is%20a%20coding%20technique%20where%20a,based%20on%20the%20condition's%20outcome"&gt;Early Return Pattern in C#&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://forum.freecodecamp.org/t/the-return-early-pattern-explained-with-javascript-examples/19364"&gt;FreeCodeCamp&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Manipulating the DOM using Javascript - traversing the DOM(part 2)✂️🕹</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Fri, 19 Aug 2022 05:56:54 +0000</pubDate>
      <link>https://forem.com/arikaturika/manipulating-the-dom-using-javascript-traversing-the-dompart-2-o7</link>
      <guid>https://forem.com/arikaturika/manipulating-the-dom-using-javascript-traversing-the-dompart-2-o7</guid>
      <description>&lt;p&gt;In the last article we learned what the DOM is and that it can be manipulated (change the nodes inside it). We also learned about one way we can target DOM nodes using different selectors (if you didn't read the first article of the series, I recommend doing so  before going any further). We basically learned how to make use of the built-in methods of the document object to access HTML elements by &lt;code&gt;class, id, tage name or query selectors.&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Another way of targeting nodes is by traversing the DOM. Traversing simply means to &lt;code&gt;move throught&lt;/code&gt; the DOM using the relation between nodes (when having access to a certain DOM node, we can reach its related nodes). This means we can stay on the same DOM level (branch) while moving up, down or even sideways. &lt;/p&gt;

&lt;p&gt;But why would we need to do that? We would think that using &lt;code&gt;document.querySelector()&lt;/code&gt; would be enough for every situation we encounter when trying to manipulate the DOM. Well, yes and no. &lt;/p&gt;

&lt;h4&gt;
  
  
  Let's think of an analogy
&lt;/h4&gt;

&lt;p&gt;Imagine you are inside of your favorite book store, where the books in a series are sorted in ascending order, from left to right. You are looking at the first Harry Potter volume that's sitting on a shelf. You're missing the first two volumes and you would like to buy them, so after you found the first one, you know the second should be next to it. Now you have two possibilities to get the second volume:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You get it directly from the shelf, since you can move a bit to the right and find it right away.&lt;/li&gt;
&lt;li&gt;You go to the cash register and ask for the person working there to look for the book in their system, tell you where in the store it's located and then go get it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which approach would be faster? The first one, of course. We found the second volume based on the position (relation) to the first one, instead of interogating the system (do a search by name). Based on this analogy, it will always be easier to move from one node to the other than doing a full search.&lt;/p&gt;

&lt;h3&gt;
  
  
  TRAVERSING THE DOM
&lt;/h3&gt;

&lt;p&gt;Before moving forward, let's write some simple HTML code so we can visualize our examples better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Traversing the DOM is fun!&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;How to&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;If you want to learn how to traverse the DOM, you should continue reading this 
   tutorial.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;We can traverse the DOM based on the relation between:&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"nodesList"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Parent nodes&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Children nodes&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Siblings nodes&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A real representation of the DOM based on the above HTML looks like bellow (if you want to generate real DOM trees, you can use &lt;a href="https://software.hixie.ch/utilities/js/live-dom-viewer/" rel="noopener noreferrer"&gt;this tool&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4oukuvxy7cmoc6f4ndi6.JPG" 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%2F4oukuvxy7cmoc6f4ndi6.JPG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can traverse the DOM based on the relation between &lt;code&gt;parent, children and sibling nodes&lt;/code&gt;. We have the first node in the tree which is called the &lt;code&gt;root node&lt;/code&gt;. This is the only node that doesn't have a parent since it's positioned on the highest level. Every other node has exactly one parent and can have any number of children. We call nodes with the same parent sibling nodes. &lt;/p&gt;

&lt;p&gt;In our example above, the simplest parent-child relationship between nodes is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;document&lt;/code&gt; is the &lt;code&gt;root node&lt;/code&gt; and it has two children: the &lt;code&gt;DOCTYPE declaration&lt;/code&gt; and the &lt;code&gt;html&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;html&lt;/code&gt; is the parent of &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;body&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;body&lt;/code&gt; is the parent of &lt;code&gt;h2&lt;/code&gt;, &lt;code&gt;p&lt;/code&gt;, &lt;code&gt;h2&lt;/code&gt; and &lt;code&gt;ul&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;ul&lt;/code&gt; is parent to the &lt;code&gt;li&lt;/code&gt;s&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, let's remember that everything in an HTML document is considered a node and we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the Document, which in itself is &lt;code&gt;a node&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;HTML elements, which are &lt;code&gt;Element Nodes&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the text inside the HTML elements, which are &lt;code&gt;Text Nodes&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;comments, which are &lt;code&gt;Comment Nodes&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  TRAVERSING BASED ON THE PARENT NODE RELATION
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;parent node&lt;/code&gt; is any node that is one level above another node, or closer to the document node in the DOM hierarchy. There are two ways to get the parent of a node: &lt;code&gt;parentNode&lt;/code&gt; and &lt;code&gt;parentElement&lt;/code&gt;. So, in our example, if we would want to get the parent node of the first &lt;code&gt;h2&lt;/code&gt;, we would say:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// first we target the h2 tag&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;headerTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;h2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;headerTwo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&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;parent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;►&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we are going to get is the &lt;code&gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&lt;/code&gt; tag, since it's  one level above the &lt;code&gt;&amp;lt;h2&amp;gt;&amp;lt;/h2&amp;gt;&lt;/code&gt; tag. If we want to go even one more level up, we can chain multiple &lt;code&gt;parentNode&lt;/code&gt; properties, like so:&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;headerTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;h2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;headerTwo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&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;parent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;►&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we 'll be targeting the &lt;code&gt;&amp;lt;html&amp;gt;&amp;lt;/html&amp;gt;&lt;/code&gt; tag since it's one level above the &lt;code&gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  TRAVERSING BASED ON THE CHILDREN NODES RELATION
&lt;/h3&gt;

&lt;p&gt;The children of a node are considered to be all nodes that are positioned one level bellow that node (that is one level of nesting).&lt;/p&gt;

&lt;p&gt;The properties that help us target children nodes are: &lt;code&gt;childNodes&lt;/code&gt;, &lt;code&gt;firstChild&lt;/code&gt;, &lt;code&gt;lastChild&lt;/code&gt;, &lt;code&gt;children&lt;/code&gt;, &lt;code&gt;firstElementChild&lt;/code&gt; and &lt;code&gt;lastElementChild&lt;/code&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  An interesting situation
&lt;/h4&gt;

&lt;p&gt;If we would want to target the children of the &lt;code&gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&lt;/code&gt; element in our example, we would probably say:&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;bodyChildren&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;childNodes&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;bodyChildren&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I assume you would expect to see a list of four items printed to the console.&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="err"&gt;►&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead you will see this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="err"&gt;►&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;tex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is happening because the &lt;code&gt;childNodes&lt;/code&gt; property is targeting all nodes, including text nodes. In our example, the indentation between the HTML lines of code are interpreted as text.&lt;/p&gt;

&lt;p&gt;I think now is the perfect time to clarify what &lt;code&gt;nodes&lt;/code&gt; (which were targeted by the childNodes property) and &lt;code&gt;elements&lt;/code&gt; (h2, p, h2, ul, which we were expecting to see in the console) are. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A node is any object represented in the DOM. Nodes can be of multiple types but the ones we are going to encounter most often are &lt;code&gt;document&lt;/code&gt;, &lt;code&gt;element&lt;/code&gt; and &lt;code&gt;text&lt;/code&gt;. So, elements are just nodes of the type &lt;code&gt;element&lt;/code&gt;. A complete list of all node types can be found &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Coming back to out example, where instead of four nodes we got back nine, this is because &lt;code&gt;childNodes&lt;/code&gt; selects all node types (including text), not only the &lt;code&gt;element&lt;/code&gt; ones, which we were interested in. &lt;/p&gt;

&lt;p&gt;If we want to select only the children of the type &lt;code&gt;node element&lt;/code&gt;, we can use the &lt;code&gt;children&lt;/code&gt;, &lt;code&gt;firstElementChild&lt;/code&gt; and &lt;code&gt;lastElementChild&lt;/code&gt; properties. So, to rewrite our 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;bodyChildren&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="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;bodyChildren&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;►&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  TRAVERSING BASED ON THE SIBLING NODES RELATION
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;sibling node&lt;/code&gt; is any node that is on the same DOM level with any other node. In our example, &lt;code&gt;h2, p, h2 and ul&lt;/code&gt; are all siblings since they are on the same DOM level. The property we can use to select sibling nodes are: &lt;code&gt;previousSibling&lt;/code&gt;, &lt;code&gt;nextSibling&lt;/code&gt;, &lt;code&gt;previousElementSibling&lt;/code&gt; and &lt;code&gt;nextElementSibling&lt;/code&gt;. Keep in mind that, just like in the case of child nodes, &lt;code&gt;previousSibling&lt;/code&gt; and &lt;code&gt;nextSibling&lt;/code&gt; select siblings of any type, whether &lt;code&gt;previousElementSibling&lt;/code&gt; and &lt;code&gt;nextElementSibling&lt;/code&gt; will only select nodes of type element.&lt;/p&gt;

&lt;p&gt;So, if we want to select the second &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; element in the &lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt;, we need to write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// we target the list based on its id&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nodesList&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// we target the first node of element type&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;firstElementChild&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// we use nextElementSibling to get to the second list item&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;thirdElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nextElementSibling&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;thirdElement&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;►&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If instead we would have used the &lt;code&gt;nextSibling&lt;/code&gt; property, the return value would have been a &lt;code&gt;text node&lt;/code&gt; since the indentation in the HTML is considered text, so we would have targeted white space (a node of type text) instead of the next list item element.&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;thirdElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;firstElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nextSibling&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;thirdElement&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;►&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we know how to traverse the DOM, in the next article we will focus on how we can actually change things in the DOM, with examples. See you next time!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Header source image: &lt;a href="https://unsplash.com/photos/oZMUrWFHOB4" rel="noopener noreferrer"&gt;Paul Esch-Laurent&lt;/a&gt; on &lt;a href="https://unsplash.com/@pinjasaur" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Refrence articles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType" rel="noopener noreferrer"&gt;Node.nodeType&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/wiki/Traversing_the_DOM" rel="noopener noreferrer"&gt;Traversing the DOM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linode.com/docs/guides/traversing-the-dom/" rel="noopener noreferrer"&gt;Traversing the Document Object Model with JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Remember to whitelist your IP when you can't connect to Mongo DB 📡💡</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Sun, 03 Jul 2022 16:51:48 +0000</pubDate>
      <link>https://forem.com/arikaturika/remember-to-whitelist-your-ip-when-you-cant-connect-to-mongo-db-1pgn</link>
      <guid>https://forem.com/arikaturika/remember-to-whitelist-your-ip-when-you-cant-connect-to-mongo-db-1pgn</guid>
      <description>&lt;p&gt;I recently started looking into backend using the MERN stack, and sometimes it happens that I can't connect to a Mongo cluster, even though it worked in the past. In 99% of the cases this is because my IP adress changed and my current IP is not &lt;code&gt;whitelisted*&lt;/code&gt;. This is nothing that can't be fixed but I never remember what causes it and I waste time until I get to the correct solution (this is pretty much like forgetting how to center a div). When I would try to connect to a cluster I would see someting like this in the terminal (in the past I think I've also seen some straight up errors, but this is the most recent message I got):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[nodemon] 2.0.16
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
connection &amp;lt;monitor&amp;gt; to 52.58.6.203:27017 closed
[nodemon] clean exit - waiting for changes before restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The connection couldn't be established because my IP address changed since I initially set up the cluster (or the last time I whitelisted my IP address). So what I would need to do is go to MongoDB Atlas and on the left side of the page, chose &lt;code&gt;Security&lt;/code&gt; and then &lt;code&gt;Network Access&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdi03ujrwnszw0p0z8aj8.JPG" 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%2Fdi03ujrwnszw0p0z8aj8.JPG" alt="Image description" width="231" height="611"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I would press &lt;code&gt;ADD IP ADDRESS&lt;/code&gt;, wait for your current IP to be whitelisted and then try to reconnect. Now everything should work again. MongoDB is pretty good at auto detecting your current IP but if the connection still can't be established, try a quick &lt;code&gt;"what's my ip"&lt;/code&gt; on Google and compare if the IP you got back is the same as the one detected by Mongo. If it's not, you might want to add it manually again.&lt;/p&gt;

&lt;p&gt;*MongoDB Atlas only allows connections to clusters from IP addresses that are matched by entries in our project’s IP whitelist&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>mongodb</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>How web browsers work - the render tree (part 7, with illustrations)💻 ⏳</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Wed, 01 Jun 2022 05:20:30 +0000</pubDate>
      <link>https://forem.com/arikaturika/how-web-browsers-work-the-render-tree-part-7-with-illustrations-24h3</link>
      <guid>https://forem.com/arikaturika/how-web-browsers-work-the-render-tree-part-7-with-illustrations-24h3</guid>
      <description>&lt;p&gt;The trees built in the parsing phase (DOM, CSSOM) are combined into something called the &lt;code&gt;render tree&lt;/code&gt;. This is used to compute the layout of all visible elements that will be painted to the screen in the end. The purpose of the &lt;code&gt;render tree&lt;/code&gt; is to make sure the content of the page will paint the elements in the correct order. It will be serverd as input to the painting process that will display the pixels on the screen.&lt;/p&gt;

&lt;p&gt;The DOM and the CSSOM are created using the HTML and the CSS files. Both of these files hold different types of information and the trees are different structures, so how does the render tree gets created?&lt;/p&gt;

&lt;h3&gt;
  
  
  COMBINING THE DOM WITH THE CSSOM
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The browser will start doing its magic at the root of the &lt;code&gt;DOM tree&lt;/code&gt; and traverse every visible node. Some of the nodes, like script or meta tags are not visible, so they are ignored. There are also nodes that will be hidden with the use of CSS (the &lt;code&gt;display: "none"&lt;/code&gt; property for example) and they will also be ignored. We are only interested in the visible nodes because only they matter for the input on the screen. &lt;/li&gt;
&lt;li&gt;For each visible node that's found in the DOM, the coresponding rules will be found in the CSSOM and they will be applied.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The result of these steps will be a &lt;code&gt;render tree&lt;/code&gt; that contains all visible nodes, with content and styles.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F56cdizmlhcvpwgjx55k7.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%2F56cdizmlhcvpwgjx55k7.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  THE LAYOUT (REFLOW) STAGE
&lt;/h3&gt;

&lt;p&gt;The render tree holds information on which nodes are displayed along with their computed styles, but not the dimensions or location of each node.&lt;/p&gt;

&lt;p&gt;What needs to be done next is calculate the exact position of those nodes within the viewport of the device (inside the browser window) and their size. This is the stage called &lt;code&gt;layout&lt;/code&gt; (in Chrome, Opera, Safari and Internet Explorer) or &lt;code&gt;reflow&lt;/code&gt; (in Firefox) but they mean the same thing. The browser starts this process at the root of the render tree and traverses it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6mzrk4t8wgh8zb1i61r.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%2Fs6mzrk4t8wgh8zb1i61r.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reflow step doesn't happen only once, but every time we change something in the DOM that affects the layout of the page, even partially. Examples of situations when the positions of the elements is recalculated are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;adding or deleting elements from the DOM&lt;/li&gt;
&lt;li&gt;resizing the browser window&lt;/li&gt;
&lt;li&gt;changing the width, the position of an element or floating it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get a very basic example of HTML, with some CSS applied inline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width,initial-scale=1"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Reflow&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width: 100%; height: 50%"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width: 50%; height: 50%"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;This is the reflow stage!&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code just says that inside the viewport we should have two &lt;code&gt;divs&lt;/code&gt;, where the second one is nested inside the first. The parent div takes up 100% of the viewport and the second 50% of the parent. This will look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnlctcq5tdzcq7v3nr9m.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%2Fjnlctcq5tdzcq7v3nr9m.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output of this process is a &lt;code&gt;box like model&lt;/code&gt; which captures exactly where each element needs to be on a screen and its size. After this step is finished, the output is ready to be passed to the next step called the &lt;code&gt;painting stage&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  THE PAINTING (REPAINTING) STAGE
&lt;/h3&gt;

&lt;p&gt;After the browser decides which nodes need to be visible and calculates their position in the viewport, it's time to paint them (render the pixels) on the screen. This phase it is also known as the &lt;code&gt;rasterization&lt;/code&gt; phase, where the browser converts each box calculated in the layout phase to actual pixels on the screen. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ki8iq5jvmprhdo3h2gt.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%2F9ki8iq5jvmprhdo3h2gt.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just like the layout stage, the painting stage doesn't happen just once but every time we change something in the appearance of the elements on the screen. Examples of these situations are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;changing the outline of an element&lt;/li&gt;
&lt;li&gt;changing background color&lt;/li&gt;
&lt;li&gt;changing opacity or visibility&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Painting means the browser needs to draw every visual part of an element to the screen, including text, colors, borders, shadows, and replaced elements like buttons and images and it needs to do it super quickly. To ensure repainting can be done even faster than the initial paint, the drawing to the screen is generally broken down into several layers. If this occurs, then compositing is necessary.&lt;/p&gt;

&lt;h3&gt;
  
  
  LAYERING AND COMPOSITING
&lt;/h3&gt;

&lt;p&gt;Traditionally, web browsers relied entirely on the CPU to render web page content. But nowadays even the smallest devices have performant GPUs, so the attention has turned on finding ways to use this piece of hardware to achieve better performance. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8l7o8i1bsy78h67kud7c.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%2F8l7o8i1bsy78h67kud7c.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Compositing is a technique to separate parts of a page into layers, painting them separately and composite as a page in a separate thread called the compositor thread. When sections of the document are drawn in different layers, overlapping each other, compositing is necessary to ensure they are drawn to the screen in the right order and the content is rendered correctly.&lt;/code&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Generally, only specific tasks get redirected to the GPU and those are the tasks that can be handled by the compositor thread alone. &lt;/p&gt;

&lt;p&gt;In order to find out which elements needs to be on which layer, the main thread walks through the layout tree and creates the &lt;code&gt;layer tree&lt;/code&gt;. By default, there's only one layer (and how these layers are implemented is browser specific) but we can find the elements that would trigger a repaint and create a separate layer for each of them. This way, the repainting should not be applied to the whole page and in addition, this process will use the GPU.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fprpama9aqqnyurkm5kph.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%2Fprpama9aqqnyurkm5kph.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we want to hint to the browser that ceratain elements should be on a separate layer, we can use the &lt;code&gt;will-change&lt;/code&gt; CSS attribute. There are actually specific properties and elements that signal the creation of a new layer. Some of these are &lt;code&gt;&amp;lt;video&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt;, and any element which has the CSS properties of &lt;code&gt;opacity&lt;/code&gt;, a 3D &lt;code&gt;transform&lt;/code&gt;, &lt;code&gt;will-change&lt;/code&gt; and a few others. These nodes will be painted onto their own layer, along with their descendants.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;KEEP IN MIND&lt;/strong&gt;&lt;br&gt;
Both of the operations discussed above, &lt;code&gt;reflow and repaint&lt;/code&gt;, are expensive, especially on devices with low processing power like phones. That's why when dealing with DOM changes we should try to optimze them (I will talk more about this in one of the future articles in my &lt;code&gt;DOM series&lt;/code&gt;). Some actions will trigger a repaint only and some actions both a reflow and a repaint. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Glosary:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The critical rendering path&lt;/strong&gt; - is the sequence of steps the browser goes through to convert the HTML, CSS and JavaScript into pixels on the screen. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Resource refrences:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://taligarsiel.com/Projects/howbrowserswork1.htm#Parsing_scripts" rel="noopener noreferrer"&gt;How browsers work&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gist.github.com/faressoft/36cdd64faae21ed22948b458e6bf04d5" rel="noopener noreferrer"&gt;DOM Performance: Reflow Repaint&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.chromium.org/developers/design-documents/gpu-accelerated-compositing-in-chrome/" rel="noopener noreferrer"&gt;GPU Accelerated Compositing in Chrome&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>html</category>
      <category>css</category>
    </item>
    <item>
      <title>What book(s) are you reading these days (and we should too)? 📖🧠</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Fri, 27 May 2022 13:38:41 +0000</pubDate>
      <link>https://forem.com/arikaturika/what-books-are-you-reading-these-days-and-we-should-too-47b8</link>
      <guid>https://forem.com/arikaturika/what-books-are-you-reading-these-days-and-we-should-too-47b8</guid>
      <description>&lt;p&gt;Currently I am reading these two books:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.goodreads.com/book/show/54968118-the-code-breaker" rel="noopener noreferrer"&gt;The Code Breaker&lt;/a&gt;. A book about DNA, RNA, gene theraphy and gene editing. It also touches on mRNA vaccines. I found it very interesting and relatively easy to follow, even though I know nothing about the field of genetics. One should read it, even if it's just for the sole purpose of expanding thier general knowledge. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5g31jowcxajobnrimy9p.jpeg" 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%2F5g31jowcxajobnrimy9p.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.goodreads.com/book/show/30363785-the-art-of-invisibility" rel="noopener noreferrer"&gt;The Art of Invisibility&lt;/a&gt;. A book written by a hacker on how to be online without leaving traces. The content is easy to follow but I personally find it tought to stomach. I think this book should be read by everyone concerned with their online presence and privacy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9jkkn89itkit7tn0rlzy.jpeg" 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%2F9jkkn89itkit7tn0rlzy.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Header source image: &lt;a href="https://unsplash.com/photos/mo3FOTG62ao" rel="noopener noreferrer"&gt;Shiromani Kant&lt;/a&gt; on &lt;a href="https://unsplash.com/" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>watercooler</category>
      <category>discuss</category>
      <category>books</category>
    </item>
    <item>
      <title>Manipulating the DOM using Javascript - how to select nodes (part 1) 👨🏼‍🔬🎯</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Wed, 18 May 2022 06:54:17 +0000</pubDate>
      <link>https://forem.com/arikaturika/manipulating-the-dom-using-javascript-how-to-select-nodes-part-1-38j</link>
      <guid>https://forem.com/arikaturika/manipulating-the-dom-using-javascript-how-to-select-nodes-part-1-38j</guid>
      <description>&lt;p&gt;In the beginning, websites were entirely made out of HTML and they could only display text (back in the early 90’s, computer monitors only supported 16 colours). The browser was downloading the HTML document, render it and in the end the content was displayed on the user's screen. There was no way to change that text, so in a way we could say it was set in stone.&lt;/p&gt;

&lt;p&gt;But people wanted more than displaying boring text so they started creating interactive sites. Internet Explorer was released and Javascript was developed in 1995. This new exciting scripting language started to be used for webpages but the interactivity provided was very limited since UIs were generated using HTML and HTML couldn't be changed after the files were downloaded (that very limited interactivity eventually became known as &lt;code&gt;DOM Level 0&lt;/code&gt; or &lt;code&gt;Legacy DOM&lt;/code&gt;). &lt;/p&gt;

&lt;p&gt;From the need to be able to change the UI after the page was loaded (add, remove, edit or move elements around in the HTML document), the first standardized version of the &lt;code&gt;DOM&lt;/code&gt; was born in 1998 and it was called &lt;code&gt;DOM Level 1&lt;/code&gt;. Changing (manipulating) the DOM suddenly opened the door for infinite possibilities. We can now create applications that are customizable by the user, that react on user's input or even update the data we see on the screen without refreshing the page (so no extra trips to the server are needed). We can drag or move elements across the screen, delete some of them or add new ones if that's what we want.&lt;/p&gt;

&lt;p&gt;Some concrete examples of DOM manipulation are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;change the content/ color of a button after we clicked on it&lt;/li&gt;
&lt;li&gt;change the content of a paragraph when hovering over it &lt;/li&gt;
&lt;li&gt;remove an item from a "To Do" list after we checked it as completed&lt;/li&gt;
&lt;li&gt;adding a new item to a "To Do" list after we typed it in an input and clicked an "Add" button&lt;/li&gt;
&lt;li&gt;navigating to a different page after submitting a form&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  THE DOM (DOCUMENT OBJECT MODEL)
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;The Document Object Model is a cross-platform and language-independent interface that treats an XML or HTML document as a tree structure wherein each node is an object representing a part of the document. The DOM represents a document with a logical tree.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simple terms this means that after the browser downloads the HTML document, it converts its content into a tree like structure called the &lt;code&gt;DOM (Document Object Model)&lt;/code&gt; and stores it in its memory. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt;&lt;br&gt;
The DOM is not a programming language and it's not a part of Javascript. It is one of the multiple web APIs built into web browsers and it has been created to be independent of any language (think of web APIs like they're collections of functions). Implementations of the DOM can be built using other scripting languages besides Javascript and every non-empty webpage has a DOM, even the ones that don't use any Javascript. You don't have to modify the DOM if your pages display only text for example, but if you want interactivity, you will probably need to work with the DOM (some of the same interactivity Javascript offers can be achieved using CSS, but this is another topic).&lt;/p&gt;

&lt;p&gt;Things might sound a bit abstract so before going any further, let's see how this DOM actually looks like. We have a very simple HTML code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Simple DOM example&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;section&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;This is a header!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;h4&amp;gt;&lt;/span&gt;This is a smaller header!&lt;span class="nt"&gt;&amp;lt;/h4&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is a paragraph!&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"mountains.jpg"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"Mountains covered in snow"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;section&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;This is another header!&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;h4&amp;gt;&lt;/span&gt;This is another small header!&lt;span class="nt"&gt;&amp;lt;/h4&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is a paragraph!&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is another paragraph!&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;/section&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"index.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below we can see how the &lt;code&gt;DOM&lt;/code&gt; for the above HTML code looks like (if you want to play around with this visual representation, you can use this &lt;a href="https://software.hixie.ch/utilities/js/live-dom-viewer/" rel="noopener noreferrer"&gt;Live DOM viewer&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fux36jitei3clsyen7ij0.JPG" 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%2Fux36jitei3clsyen7ij0.JPG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this is the tree-like structure the HTML gets translated to. The tree is made out of &lt;code&gt;nodes&lt;/code&gt;. Some nodes represent HTML elements (&lt;code&gt;HTML, HEAD, BODY, SECTION&lt;/code&gt; etc) and other represent text (the ones represented as &lt;code&gt;#text&lt;/code&gt;). A complete list of all &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType" rel="noopener noreferrer"&gt;node types&lt;/a&gt; can be found &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Based on its position in the tree, a node can be a:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Root node&lt;/strong&gt;&lt;br&gt;
 This is the top node of the tree, which in the case of HTML is the &lt;code&gt;HTML node&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;Parent node&lt;/strong&gt;&lt;br&gt;
A node which has other node(s) inside it. For example, &lt;code&gt;BODY&lt;/code&gt; is the parent node of all nodes inside it.&lt;br&gt;
&lt;strong&gt;Child node&lt;/strong&gt;&lt;br&gt;
A node directly inside another node. In our example, the &lt;code&gt;H1 node&lt;/code&gt; is the child of the &lt;code&gt;SECTION node&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;Sibling nodes&lt;/strong&gt;&lt;br&gt;
These are nodes that are found on the same level in the DOM. &lt;code&gt;H1, H4, P and IMG nodes&lt;/code&gt; are all siblings since they are on the same level inside the &lt;code&gt;SECTION node&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;Descendant node&lt;/strong&gt;&lt;br&gt;
This is a node that can be found anywhere inside another node. &lt;code&gt;H4&lt;/code&gt; is for example the descendant node of the &lt;code&gt;BODY&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  MANIPULATING THE DOM
&lt;/h3&gt;

&lt;p&gt;What does manipulating the DOM means? It means we can change the nodes in the tree we've just seen, making use of APIs that can control the HTML and the styling of a page. Each node has its own properties and methods that can be manipulated using Javascript. &lt;/p&gt;

&lt;p&gt;All the properties, methods and events available for manipulating and creating web pages are organized into objects that we're going to call interfaces. There are many DOM interfaces working together but the ones that we're going to use most often are &lt;code&gt;Window&lt;/code&gt; and &lt;code&gt;Document&lt;/code&gt;. A &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model" rel="noopener noreferrer"&gt;complete list of the DOM interfaces&lt;/a&gt; can be found &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Window&lt;/strong&gt; - The Window interface represents a window containing a DOM document (an open window in a browser). It holds the highest position in the DOM hierarchy, as it is a parent of the &lt;code&gt;Document object&lt;/code&gt; and all its children .&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Document&lt;/strong&gt; - The Document interface represents any web page loaded in the browser and serves as an entry point into the web page's content, which is the DOM tree. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsmo2222h2h28iydfzuof.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%2Fsmo2222h2h28iydfzuof.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.TARGETING NODES&lt;/strong&gt;&lt;br&gt;
In order to interact with any node in the tree, we first need to target (select) it. We can do this using one of the multiple methods the DOM API offers (notice that all these methods are called on the &lt;code&gt;document&lt;/code&gt; object using the dot notation):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;getElementById()&lt;/strong&gt;. We select and HTML element making use of its &lt;code&gt;id attribute&lt;/code&gt;. It returns an element matching the specified ID, or null if no matching element was found in the document.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;idSelector&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;will&lt;/span&gt; &lt;span class="nx"&gt;be&lt;/span&gt; &lt;span class="nx"&gt;selected&lt;/span&gt; &lt;span class="nx"&gt;based&lt;/span&gt; &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elementById&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;idSelector&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;elementById&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// will return &amp;lt;div id="idSelector"&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;getElementsByClassName()&lt;/strong&gt;. We select and HTML element based on its &lt;code&gt;class attribute&lt;/code&gt;. This method returns a live HTMLCollection (an array-like list) of HTML elements, possibly of length 0 if no matching elements are found.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;classSelector&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;classSelector&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt; &lt;span class="nx"&gt;too&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;classSelector&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;am&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;guessed&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elementByClassName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByClassName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;classSelector&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;elementByClassName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// will return HTMLCollection {0: HTMLParagraphElement {...}, &lt;/span&gt;
&lt;span class="c1"&gt;// 1: HTMLParagraphElement {...}, &lt;/span&gt;
&lt;span class="c1"&gt;// 2: HTMLParagraphElement {...}}&lt;/span&gt;
&lt;span class="c1"&gt;// 0:&amp;lt;p class="classSelector"&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 1:&amp;lt;p class="classSelector"&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 2:&amp;lt;p class="classSelector"&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;getElementsByTagName()&lt;/strong&gt;. We target HTML elements based on their &lt;code&gt;tag names&lt;/code&gt;. This method returns a live HTMLCollection  of all matching HTML elements, possibly of length 0 if no match is found.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;fun&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;like&lt;/span&gt; &lt;span class="nx"&gt;writing&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="nx"&gt;article&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h4&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;DOM&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;so&lt;/span&gt; &lt;span class="nx"&gt;interesting&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h4&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elementByTagName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p&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;elementByTagName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// will return HTMLCollection {0: HTMLParagraphElement {...}, &lt;/span&gt;
&lt;span class="c1"&gt;// 1: HTMLParagraphElement {...}}&lt;/span&gt;
&lt;span class="c1"&gt;// 0:&amp;lt;p &amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 1:&amp;lt;p &amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;getElementsByName()&lt;/strong&gt;. This method returns a live NodeList Collection of elements with a given &lt;code&gt;name attribute&lt;/code&gt; in the document. If no match is found, the collection will be empty.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;someInput&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elementsByName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;someInput&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;elementsByName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// will return NodeList {0: HTMLInputElement {...}}&lt;/span&gt;
&lt;span class="c1"&gt;// 0:&amp;lt;input type="text" name="someInput"&amp;gt;&amp;lt;/input&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;querySelector()&lt;/strong&gt;. Method that returns the first element within the document that matches the &lt;code&gt;specified selector&lt;/code&gt;, or &lt;code&gt;group of selectors&lt;/code&gt;. If no matches are found, null is returned. We can provide any selector we want as an argument (a class, an ID etc).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;divClass&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;just&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;thisIsAnId&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;another&lt;/span&gt; &lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;numberOnePara&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;just&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;paragraph&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;querySelectionByClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.divClass&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;querySelectionByClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// will return &amp;lt;div class="divClass"&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;querySelectionById&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#thisIsAnId&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;querySelectionById&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// will return &amp;lt;div id="thisIsAnId"&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;querySelectorByName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[name='numberOnePara']&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;querySelectorByName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// will return &amp;lt;p name="numberOnePara"&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;querySelectorAll()&lt;/strong&gt;. This method returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors. The NodeList will be empty if no matches are found.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Paragraph&lt;/span&gt; &lt;span class="nx"&gt;number&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="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Paragraph&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Paragraph&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Paragraph&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Paragraph&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queryAllParas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p&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;queryAllParas&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// will return NodeList {0: HTMLParagraphElement {...}, &lt;/span&gt;
&lt;span class="c1"&gt;// 1: HTMLParagraphElement {...}, &lt;/span&gt;
&lt;span class="c1"&gt;// 2: HTMLParagraphElement {...}, &lt;/span&gt;
&lt;span class="c1"&gt;// 3: HTMLParagraphElement {...},&lt;/span&gt;
&lt;span class="c1"&gt;// 4: HTMLParagraphElement {...}}&lt;/span&gt;
&lt;span class="c1"&gt;// 0:&amp;lt;p &amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 1:&amp;lt;p &amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 2:&amp;lt;p &amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 3:&amp;lt;p &amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// 4:&amp;lt;p &amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  GLOSSARY
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;HTML Collection&lt;/strong&gt; - in simple terms, an HTML Collection is an array-like object that holds HTML elements extracted from a document. An HTML Collection can contain only &lt;code&gt;Element Nodes&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;NodeList&lt;/strong&gt; - It's a collection of nodes. It is similar to an HTML Collection but it can contain all type of nodes (&lt;code&gt;Element, Text and Attribute&lt;/code&gt;) not only Element Nodes.&lt;br&gt;
&lt;strong&gt;Live HTMLCollection&lt;/strong&gt; - The collection updates when the DOM updates.&lt;br&gt;
&lt;strong&gt;Static HTML Collection&lt;/strong&gt; - If the DOM updates, the changes are not reflected in the collection.&lt;br&gt;
&lt;strong&gt;Live NodeList&lt;/strong&gt; - The collection updates when the DOM updates.&lt;br&gt;
&lt;strong&gt;Static NodeList&lt;/strong&gt; -  If the DOM updates, the changes are not reflected in the collection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resource refrences:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction" rel="noopener noreferrer"&gt;Introduction to the DOM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/how-to-manipulate-the-dom-beginners-guide/" rel="noopener noreferrer"&gt;How to Manipulate the DOM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Manipulating_documents" rel="noopener noreferrer"&gt;Manipulating documents&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Header image source: Jackson So/@jacksonsophat on &lt;a href="https://unsplash.com/photos/wUbNvDTsOIc" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>html</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How web browsers work - creating the accessibility tree (part 6, with illustrations)🌴🐱‍💻</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Sun, 08 May 2022 09:35:52 +0000</pubDate>
      <link>https://forem.com/arikaturika/how-web-browsers-work-creating-the-accessibility-tree-part-6-with-illustrations-2hl2</link>
      <guid>https://forem.com/arikaturika/how-web-browsers-work-creating-the-accessibility-tree-part-6-with-illustrations-2hl2</guid>
      <description>&lt;p&gt;Besides all these trees we've been talking about until now (DOM, CSSOM and AST), browsers also build something called the &lt;code&gt;accessibility tree&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Accessibility (often abbreviated to A11y — as in, "a", then 11 characters, and then "y") in web development means enabling as many people as possible to use websites, even when those people's abilities are limited in some way. For many people, technology makes things easier. For people with disabilities, technology makes things possible. Accessibility means developing content to be as accessible as possible, no matter an individual's physical and cognitive abilities and how they access the web (ACT).&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  6. BUILDING THE ACCESSIBILITY TREE
&lt;/h2&gt;

&lt;p&gt;In general, disabled users can and do use web pages with a variety of assistive technologies. They use screenreaders, magnifiers, eye tracking, voice commands and more. In order for any of these technologies to work, they need to be able to access the page's content. And since they can't read the DOM directly, the ACT comes into play. &lt;/p&gt;

&lt;p&gt;The accessibility tree is built using the DOM and it will be later used by the assistive devices to parse and interpret the content of the webpage we are visiting. ACT is like a semantic version of the DOM and it gets updated every time the DOM gets updated. Each DOM element that needs to be exposed to assistive technologies will have a corespondent node in the ACT. Until the ACT is not built, the content is not accessible to screen readers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgugy39zsayyjq844i5t2.jpg" 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%2Fgugy39zsayyjq844i5t2.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To view what the accessibility tree actually looks like, you can use Google Chrome by going to a page of your choosing. Open the debugger (F12) and go to the &lt;code&gt;Elements&lt;/code&gt; tab. From there, on the right side you can select the &lt;code&gt;Accessibility&lt;/code&gt; pane.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd9mikz499vy8r2jyqwi1.JPG" 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%2Fd9mikz499vy8r2jyqwi1.JPG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I went to Google and inspected the search input and this is what I got in the &lt;code&gt;Accessibility pane&lt;/code&gt; under &lt;code&gt;Computed&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzeho6dy87c6uewteudn.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%2Fxzeho6dy87c6uewteudn.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The importance of using semantic HTML is beyond the scope of this article, but as developers, we should all keep in mind that the websites we build should be made availabe to everyone who wishes to use them. If you want to read more on the subject, a good introductory article into web accessibility can be found &lt;a href="https://www.w3.org/WAI/fundamentals/accessibility-intro/#:~:text=Web%20accessibility%20means%20that%20websites,and%20interact%20with%20the%20Web" rel="noopener noreferrer"&gt;here&lt;/a&gt;. According to &lt;a href="https://www.a11ysig.org/" rel="noopener noreferrer"&gt;The Internet Society Accessibility Special Interest Group&lt;/a&gt;, there are now over 1.3 billion people worldwide – about 15 percent of the world's population – that experience some form of disability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Resource refrences:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/WAI/fundamentals/accessibility-intro/" rel="noopener noreferrer"&gt;w3.org - Introduction to Web Accessibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility" rel="noopener noreferrer"&gt;MDN Web Docs - Accessibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles" rel="noopener noreferrer"&gt;WAI-ARIA Roles&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How web browsers work - executing the Javascript (part 5, with illustrations)💻🌠</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Wed, 04 May 2022 19:11:03 +0000</pubDate>
      <link>https://forem.com/arikaturika/how-web-browsers-work-executing-the-javascript-part-5-with-illustrations-21ok</link>
      <guid>https://forem.com/arikaturika/how-web-browsers-work-executing-the-javascript-part-5-with-illustrations-21ok</guid>
      <description>&lt;p&gt;While the CSS is being parsed and the CSSOM created, other assets, including JavaScript files, are downloaded. This is happening thanks to the &lt;code&gt;preloader&lt;/code&gt; we mentioned in the previous articles.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;A preloader is like a parser that scans the HTML file while the main parser is processing the HTML code. Its role is to look for  resources like stylesheets, scripts or images (that also need to be retrieved from a server) and request them. Hopefully, by the time the HTML is parsed, those resources are already downloaded and ready to be processed.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  5. JAVASCRIPT EXECUTION
&lt;/h2&gt;

&lt;p&gt;So, after we get the Javascript file from the server, the code is interpreted, compiled, parsed and executed. The computer can't understand Javascript code, only the browser can. The JS code needs to be translated into something the computer can work with and this is the job of the &lt;code&gt;Javascript browser engine&lt;/code&gt; (not to be confused with the &lt;code&gt;browser engine&lt;/code&gt;). Depending on the browser, JS engines can have different names and work differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Javascript engines
&lt;/h3&gt;

&lt;p&gt;A javascript engine (sometimes also called an &lt;code&gt;ECMAScript engine&lt;/code&gt;) is a piece of software that executes (runs) Javascript code in the browser, and not only (the V8 engine is a core component of the Node.js environment, for example).&lt;/p&gt;

&lt;p&gt;JavaScript engines are typically developed by web browser vendors, and every major browser has one. We said the most used browsers as of today are &lt;code&gt;Chrome&lt;/code&gt;, &lt;code&gt;Safari&lt;/code&gt;, &lt;code&gt;Edge&lt;/code&gt; and &lt;code&gt;Firefox&lt;/code&gt;. Each one is using a different Javascript engine and these are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;V8&lt;/strong&gt;&lt;br&gt;
V8 is Google’s  high-performance JavaScript engine. It is written in C++ and it's used in Chrome and in Node.js, among others. It implements &lt;code&gt;ECMAScript&lt;/code&gt; (a JavaScript standard meant to ensure the interoperability of web pages across different web browsers) and &lt;code&gt;WebAssembley&lt;/code&gt;. It implements ​ECMA-262.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JavaScriptCore&lt;/strong&gt;&lt;br&gt;
JavaScriptCore is the built-in JavaScript engine for WebKit and it powers the Safari browser, Mail and other applications used on macOS. It currently implements ​ECMAScript as in ​ECMA-262 specification. Also it also called &lt;code&gt;SquirrelFish&lt;/code&gt; or &lt;code&gt;SquirrelFish Extreme&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chakra&lt;/strong&gt;&lt;br&gt;
Chakra is a Javascript engine developed by Microsoft for its Microsoft Edge web browser and other Windows applications. It implements ECMAScript 5.1, and has partial (growing) support for ECMAScript 6. It is written in C++.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SpiderMonkey&lt;/strong&gt;&lt;br&gt;
SpiderMonkey is Mozilla's Javascript and WebAssembly Engine. It is written in C++, Javascript and Rust and it is used to power Firefox, Servo and other projects. &lt;/p&gt;

&lt;p&gt;In the beginning, Javascript engines were simple interpreters. The modern browsers we use today have the capacity to do something called &lt;code&gt;Just-In-Time (JIT) compilation&lt;/code&gt;, a mix between &lt;code&gt;compilation&lt;/code&gt; and &lt;code&gt;interpretation&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compilation&lt;/strong&gt;&lt;br&gt;
During compilation, a piece of software called &lt;code&gt;the compiler&lt;/code&gt; takes the code written in a high-level language and converts it into machine code, all at once. An intermediate file (called an &lt;code&gt;object file&lt;/code&gt;) is created and that file can run on any machine. After these steps are taken, the code can be executed (imediately after, sometimes in the future or never). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqtlubp70yhfm734zj9f9.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%2Fqtlubp70yhfm734zj9f9.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interpretation&lt;/strong&gt;&lt;br&gt;
During interpretation, the interpreter is going through the Javascript code line by line and executes it imediately. No compilation is taking place so no Object Code is created (the output of the code is created by the interpreter itself, using its internal mechanisms). Older versions of Javascript use this type of code execution.&lt;br&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%2Fkvw7pmbm6c7temfb2ov3.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%2Fkvw7pmbm6c7temfb2ov3.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JIT Compilation&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Just in time compilation&lt;/code&gt; is a feature of an interpreter for a given language and it tries to take advantage of both  &lt;code&gt;compilation and interpretation&lt;/code&gt;. Whether during &lt;code&gt;pure compilation&lt;/code&gt;, the code is translated before it is executed, in &lt;code&gt;JIT compilation&lt;/code&gt; the code is translated &lt;code&gt;while it is being executed&lt;/code&gt; (at run time). So we could say that source code is converted to machine code on the fly. Newer versions of Javascript use this type of code execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcoqiinfyz48luucra2a4.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%2Fcoqiinfyz48luucra2a4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A very important aspect about JIT compilation is that it will compile the source code into machine code instructions of the running machine. This means that the resulting machine code is optimized for the running machine’s CPU architecture. &lt;/p&gt;

&lt;p&gt;In super simple terms, these three processes could be resumed to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compiler: translates the code&lt;/li&gt;
&lt;li&gt;Interpreter: runs the code &lt;/li&gt;
&lt;li&gt;JIT Compiler: translates while running the code &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Today, the line between the terms &lt;code&gt;compilation&lt;/code&gt; and &lt;code&gt;interpretation&lt;/code&gt; has become very blurry so the subject can be extensively debated. If you want to know more about these processes, you could read &lt;a href="https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers/" rel="noopener noreferrer"&gt;this article on Mozilla Hacks&lt;/a&gt; for starters.&lt;/p&gt;

&lt;p&gt;Notice I mentioned older and newer version of Javascript. Browsers that don't support newer versions of the language will interpret the code while the ones that do will use some version of JIT to execute the code (the V8, Chakra JavaScriptCore and SpiderMonkey engines all use JIT). The truth is that even though Javascript is an interpreted language (it does not need compilation), most browsers today will use JIT compilation to run the code, instead of pure interpretation. &lt;/p&gt;

&lt;h3&gt;
  
  
  How is the Javascript code processed
&lt;/h3&gt;

&lt;p&gt;When Javascript code enters the Javascript engine it gets parsed as a first step. This means the code is read, and while this is happening, the code is transformed into a data structure called the &lt;code&gt;Abstract Syntax Tree&lt;/code&gt; (AST). The code will be split into pieces that are relevant to the language (like &lt;code&gt;function&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; keywords) and then all these pieces will build the Abstract Synta Tree.&lt;/p&gt;

&lt;p&gt;Let's say we have a file containing a program that only does one thing, and that's to define a variable:&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This is how this incredibly super simple line of code would look as an Abstract Syntax Tree (I am using &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/parser-7.16.12):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwsh5a1fdgq4uspeos108.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%2Fwsh5a1fdgq4uspeos108.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want convert some Javascript to an Abstract Syntax Tree yourself, you can use this &lt;a href="https://astexplorer.net/" rel="noopener noreferrer"&gt;tool&lt;/a&gt;. The AST resulted after writing my varible is actually much bigger and has more nodes that are hidden in the screen shot.&lt;/p&gt;

&lt;p&gt;After the AST has been built, it gets translated into machine code and executed right away, since modern Javascript uses Just-In-Time compilation. The execution of this code will be done by the Javascript engine, making use of something called the "call stack".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function etc.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Refrence materials:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://spidermonkey.dev/" rel="noopener noreferrer"&gt;SpiderMonkey&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/chakra-core/ChakraCore" rel="noopener noreferrer"&gt;ChakraCore&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.apple.com/documentation/javascriptcore" rel="noopener noreferrer"&gt;JavaScriptCore&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://v8.dev/" rel="noopener noreferrer"&gt;v8&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodejs.dev/learn/the-v8-javascript-engine" rel="noopener noreferrer"&gt;Node&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Call_stack" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>css</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How web browsers work - parsing the CSS (part 4, with illustrations)⏳🌐</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Fri, 29 Apr 2022 14:31:14 +0000</pubDate>
      <link>https://forem.com/arikaturika/how-web-browsers-work-parsing-the-css-part-4-with-illustrations-4c</link>
      <guid>https://forem.com/arikaturika/how-web-browsers-work-parsing-the-css-part-4-with-illustrations-4c</guid>
      <description>&lt;p&gt;After the HTML has been parsed, it's time to &lt;code&gt;parse the CSS&lt;/code&gt; (found in in external CSS files and in style elements) and build the &lt;code&gt;CSSOM tree&lt;/code&gt; (CSS Object Model). &lt;/p&gt;

&lt;h2&gt;
  
  
  4. CSS PARSING
&lt;/h2&gt;

&lt;p&gt;When the browser encounters a CSS stylesheet, be it external or embeded, it needs to parse the text into something it can use for styling the layouts. The data structure that the browser turns the CSS into is called the CSSOM. The DOM and the CSSOM follow similar concepts, in the sense that they are both trees, but they are &lt;code&gt;different data structures&lt;/code&gt;. Just like building the DOM out of our HTML, building the CSSOM out of CSS is considered a render-blocking process. &lt;/p&gt;

&lt;h3&gt;
  
  
  Tokenization &amp;amp; building the CSSOM
&lt;/h3&gt;

&lt;p&gt;Similar to HTML parsing, CSS parsing starts with tokenization. The CSS parser takes the bytes and converts them into characters, then tokens, then nodes and finally they are linked into the CSSOM. The browser does something called &lt;code&gt;selector machting&lt;/code&gt; which means that each set of styles will be matched against all nodes (elements) on the page. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqyiwv33dqna9wai2p2wm.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%2Fqyiwv33dqna9wai2p2wm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The browser starts with the most general rule applicable to a node (e.g: if a node it's the child of the body element, then all body styles are inherited by that node) and then recursively refines the computed styles by applying more specific rules. This is why we say that the style rules are &lt;code&gt;cascading&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Imagine we have the HTML and CSS below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;

&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 

&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;32px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;section&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;tomato&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;section&lt;/span&gt; &lt;span class="nc"&gt;.mainTitle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;margin-left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="m"&gt;8px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;The CSSOM for this code would look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6udct34olw9ekkwqbxws.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%2F6udct34olw9ekkwqbxws.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that in the schema above, the nested elements have both &lt;code&gt;inherited styles&lt;/code&gt; (from the parent - e.g: &lt;code&gt;h1&lt;/code&gt; inherits its color from the &lt;code&gt;body&lt;/code&gt; and the &lt;code&gt;section&lt;/code&gt; inherits its font-size from the &lt;code&gt;body&lt;/code&gt;) and &lt;code&gt;their own styles&lt;/code&gt; (which can overwrite rules inherited from the parent or not - e.g: &lt;code&gt;p&lt;/code&gt; overwrites both the color and the font-size inherited from the &lt;code&gt;div&lt;/code&gt; and &lt;code&gt;mainTitle&lt;/code&gt; doesn't get its left margin from a parent node).&lt;/p&gt;

&lt;p&gt;Since we can have multiple sources for our CSS and they can contain rules that apply to the same node, the browser must decide which rule will apply in the end. That's when &lt;code&gt;specificity&lt;/code&gt; comes into play and if you want to read more about it, you can visit &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity" rel="noopener noreferrer"&gt;this page&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Imagine you're in the airport and you're looking for your friend John. If you want to find him by calling out his name, you could call for "John". Chances are that more than one John will be in the airport at the same time, so they might all respond. A better approach would be to call your friend using his full name, so that when you shout "John Doe", you'll have better chances to finding him, since "John Doe" is more specific than just "John". &lt;/p&gt;

&lt;p&gt;On the same note, let's say we have this element:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://dev.to/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;This is just a link!&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;and these CSS styles:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;

&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;p&lt;/span&gt;  &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&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;Which rule do you think will the browser apply? The answer is the second rules, since &lt;code&gt;all anchor tags inside a paragraph&lt;/code&gt; selector combination has more specificity than just &lt;code&gt;all anchor tags&lt;/code&gt; selector. If you want to play around with specificity, you can use this &lt;a href="https://specificity.keegan.st/" rel="noopener noreferrer"&gt;Specificity calculator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IMPORTANT&lt;/strong&gt;&lt;br&gt;
CSS rules are read from right to left, meaning that if we have something like: &lt;code&gt;section p { color: blue; }&lt;/code&gt;, the browser will first look for all &lt;code&gt;p&lt;/code&gt; tags on the page and then it will look if any of those &lt;code&gt;p&lt;/code&gt; tags have a &lt;code&gt;section&lt;/code&gt; tag as a parent. If that's the case, it will apply the CSS rule. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Refrence materials:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/Style/CSS20/history.html" rel="noopener noreferrer"&gt;A brief history of CSS until 2016&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/Performance/How_browsers_work#navigation" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How web browsers work - parsing the HTML (part 3, with illustrations)📜🔥</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Mon, 25 Apr 2022 16:23:20 +0000</pubDate>
      <link>https://forem.com/arikaturika/how-web-browsers-work-parsing-the-html-part-3-with-illustrations-45fi</link>
      <guid>https://forem.com/arikaturika/how-web-browsers-work-parsing-the-html-part-3-with-illustrations-45fi</guid>
      <description>&lt;p&gt;Until now we discussed &lt;code&gt;navigation&lt;/code&gt; and &lt;code&gt;data fetching&lt;/code&gt;. Today we're going to talk about &lt;code&gt;parsing&lt;/code&gt; in general and &lt;code&gt;HTML parsing&lt;/code&gt; in particular. &lt;/p&gt;

&lt;h2&gt;
  
  
  3. HTML PARSING
&lt;/h2&gt;

&lt;p&gt;We saw how after the initial request to the server, the browser receives a response containing the &lt;code&gt;HTML&lt;/code&gt; resources of the webpage we are trying to access (the first chunk of data). Now the job of the browswer will be to start &lt;code&gt;parsing&lt;/code&gt; the data.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Parsing means analyzing and converting a program into an internal format that a runtime environment can actually run&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, parsing means taking the code we write as text (HTML, CSS) and transform it into something that the browser can work with. The &lt;code&gt;parsing&lt;/code&gt; will be done by the &lt;code&gt;browser engine&lt;/code&gt; (not to be confused with the the &lt;code&gt;Javascript engine&lt;/code&gt; of the browser). &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;browser engine&lt;/code&gt; is a core component of every major browser and it's main role is to combine structure (HTML) and style (CSS) so it can draw the web page on our screens. It is also responsible to find out which pieces of code are interactive. We should not think about it like a separate piece of software but as being part of a bigger sofware (in our case, the browser). &lt;/p&gt;

&lt;p&gt;There are many browser engines in the wild but the majority of the browsers use one of these three actively developed full engines:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gecko&lt;/strong&gt;&lt;br&gt;
 It was developed by Mozilla for Firefox. In the past it used to power several other browsers but at the moment, besides Firefox, Tor and Waterfox are the only ones still using Gecko.  It is written in &lt;code&gt;C++ and JavaScript&lt;/code&gt;, and since 2016, additionally in &lt;code&gt;Rust&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WebKit&lt;/strong&gt;&lt;br&gt;
 It's primarily developed by Apple for Safari. It also powers GNOME Web (Epiphany) and Otter. (surprinsingly enough, on iOS, all browsers including Firefox and Chrome, are also powered by WebKit). It it written in &lt;code&gt;C++&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blink, part of Chromium&lt;/strong&gt;&lt;br&gt;
Beginning as a fork of WebKit, it's primarily developed by Google for Chrome. It also powers Edge, Brave, Silk, Vivaldi, Opera, and most other browser projects (some via QtWebEngine). It is written in &lt;code&gt;C++&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that we understand who's going to do the &lt;code&gt;parsing&lt;/code&gt;, let's see what happens exactly after we receive the first HTML document from the server. Let's assume the document looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="cp"&gt;&amp;lt;!doctype HTML&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;This is my page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;This is my page&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;This is a H3 header.&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is a paragraph.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;This is another paragraph,&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Even if the request page's HTML is larger than the initial 14KB packet, the browser will begin parsing and attempting to render an experience based on the data it has. &lt;code&gt;HTML parsing&lt;/code&gt; involves two steps: &lt;code&gt;tokenization&lt;/code&gt; and &lt;code&gt;tree construction&lt;/code&gt; (building something called the &lt;code&gt;DOM Tree (Document Object Model)&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Tokenization
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;It is the lexical analysis and it converts some input into tokens (basic components of source code). Imagine we would take an English text and break it down into words, where the words would be the tokens.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What results at the end of the tokenization process is a series of zero or more of the following tokens: DOCTYPE, start tag (&lt;code&gt;&amp;lt;tag&amp;gt;&lt;/code&gt;), end tag (&lt;code&gt;&amp;lt;/tag&amp;gt;&lt;/code&gt;), self-closing tag (&lt;code&gt;&amp;lt;tag/&amp;gt;&lt;/code&gt;), attribute names, values, comments, characters, end-of-file or plain text content within an element. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foy2l781kaik9tq6xvs4d.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%2Foy2l781kaik9tq6xvs4d.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Building the DOM
&lt;/h3&gt;

&lt;p&gt;After the first token gets created, &lt;code&gt;tree building&lt;/code&gt; starts. This is essentially creating a &lt;code&gt;tree like structure&lt;/code&gt; (called the Document Object Model) based on the previously parsed tokens.&lt;/p&gt;

&lt;p&gt;The DOM tree describes the content of the HTML document. The &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; element is the first tag and root node of the document tree. The tree reflects the relationships and hierarchies between different tags. We have &lt;code&gt;parent nodes&lt;/code&gt; and tags nested within other tags are &lt;code&gt;child nodes&lt;/code&gt;. The greater the number of nodes, the longer it will takes to build the DOM tree. Below is the DOM Tree for the HTML document example we got from the server:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8qdomt3z4u21pex1hhbw.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%2F8qdomt3z4u21pex1hhbw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In reality, the DOM is more complex than what we see in that schema, but I kept it simple for a better undestanding (also, we'll talk in more detail about the DOM and its importance in a future article).&lt;/p&gt;

&lt;p&gt;This building stage is &lt;code&gt;reentrant&lt;/code&gt;, meaning that while one token is handled, the tokenizer might be resumed, causing further tokens to be emitted and processed before the first token's processing is complete. From bytes until the DOM is created, the complete process would look like something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9h659kk44fxd4ke0soez.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%2F9h659kk44fxd4ke0soez.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The parser works line by line, from top to bottom. When the parser will encounter non-blocking resources (for example images), the browser will request those images from the server and continue parsing. On the other hand, if it encounters blocking-resources (CSS stylesheets, Javascrpt files added in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section of the HTML or fonts added from a CDN ), the parser will stop execution until all those blocking resources are downloaded. That's why, if yu're working with Javascript it is recommended to add your &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags at the end of the HTML file, or if you want to keep them in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; tag, you should add to them the &lt;code&gt;defer&lt;/code&gt; or &lt;code&gt;async&lt;/code&gt; attribute (&lt;code&gt;async&lt;/code&gt; allows for asynchronous as soon as the script is downloaded and &lt;code&gt;defer&lt;/code&gt; allows execution only after the whole document has been parsed.). &lt;/p&gt;

&lt;h3&gt;
  
  
  Pre-loaders and making the page faster
&lt;/h3&gt;

&lt;p&gt;Internet Explorer, WebKit and Mozilla all implemented pre-loaders in 2008 as a way of dealing with blocking resources, especially scripts (we said earlier, that when encountering a script tag, the HTML parsing would stop until the script is downloaded and executed). &lt;/p&gt;

&lt;p&gt;With a &lt;code&gt;pre-loader&lt;/code&gt;, when the browser is stuck on a script, a second ligher parser is scanning the HTML for resources that need to be retrieved (stylesheets, scripts etc). The pre-loader then starts retrieving these resources in the background with the aim that by the time the main HTML parser reaches them they may have already been downloaded (in case these resources were already cached, this step is skipped).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Refrence materials:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Parse" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://html.spec.whatwg.org/multipage/parsing.html" rel="noopener noreferrer"&gt;whatwg.org&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://javascript.info/dom-nodes" rel="noopener noreferrer"&gt;Javascript Info&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>css</category>
      <category>html</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How web browsers work - fetching data (part 2, with illustrations)🚀</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Sat, 23 Apr 2022 11:19:19 +0000</pubDate>
      <link>https://forem.com/arikaturika/how-web-browsers-work-part-2-with-illustrations-1gn5</link>
      <guid>https://forem.com/arikaturika/how-web-browsers-work-part-2-with-illustrations-1gn5</guid>
      <description>&lt;p&gt;In the last article we talked about &lt;code&gt;navigation&lt;/code&gt;, the first step the browser takes to display the websites. Today we'll move onto the next step and see how &lt;code&gt;resources get fetched&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. DATA FETCHING
&lt;/h2&gt;

&lt;h3&gt;
  
  
  HTTP Request
&lt;/h3&gt;

&lt;p&gt;After we established a secure connection with the server, the browser will send an initial &lt;code&gt;HTTP GET request&lt;/code&gt;. First, the browser will request the markup (&lt;code&gt;HTML&lt;/code&gt;) document for the page. It will do this using the HTTP protocol.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;HTTP (Hypertext Transfer Protocol) is a protocol for fetching resources such as HTML documents. It is the foundation of any data exchange on the Web and it is a client-server protocol, which means requests are initiated by the recipient, usually the Web browser.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&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%2Fc842u6z95fugi51kkxgo.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%2Fc842u6z95fugi51kkxgo.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The method&lt;/strong&gt; - e.g: POST, GET, PUT, PATCH, DELETE etc&lt;br&gt;
&lt;strong&gt;URI&lt;/strong&gt; - stands for &lt;code&gt;Uniform Resource Identifier&lt;/code&gt;. URIs and used to identify abstract or physical resources on the Internet, resources like websites or email addresses. An URI can have up to 5 parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;scheme: used to say what protocol is being used &lt;/li&gt;
&lt;li&gt;authority: used to identify the domain&lt;/li&gt;
&lt;li&gt;path: used to show the exact path to the resource&lt;/li&gt;
&lt;li&gt;query: used to represent a request action&lt;/li&gt;
&lt;li&gt;fragment: used to refer to a part of a resource&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// URI parts&lt;/span&gt;
&lt;span class="nx"&gt;scheme&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;// authority path ? query # fragment&lt;/span&gt;

&lt;span class="c1"&gt;//URI example&lt;/span&gt;
&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//example.com/users/user?name=Alice#address&lt;/span&gt;

&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;// scheme name&lt;/span&gt;
&lt;span class="nx"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;com&lt;/span&gt; &lt;span class="c1"&gt;// authority&lt;/span&gt;
&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="c1"&gt;// path&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;Alice&lt;/span&gt; &lt;span class="c1"&gt;// query&lt;/span&gt;
&lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="c1"&gt;// fragment&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;HTTP header fields&lt;/strong&gt; - are a list of strings sent and received by both browser and server on every HTTP request and response (they are usually invisible for the end-user). In the case of requests, they contain more information about the resource to be fetched or about the browser requesting the resource.&lt;/p&gt;

&lt;p&gt;If you want to see how these headers look like, go to Chrome and open the Developer tools (F12). Go to the Network tab and select &lt;code&gt;FETCH/XHR&lt;/code&gt;. In the screen shot bellow I just made a Google search for &lt;code&gt;Palm Springs&lt;/code&gt; and this is how the request headers looked like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fls9p1hjw8n3q328d72qu.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%2Fls9p1hjw8n3q328d72qu.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP Response
&lt;/h3&gt;

&lt;p&gt;Once the server receives the request, it will process it and reply with an &lt;code&gt;HTTP response&lt;/code&gt;. Attached to the body of the response we can find all relevant headers and the contents of the HTML document we requested. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe4s4md4r02wmb3t8y9ef.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%2Fe4s4md4r02wmb3t8y9ef.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Status code&lt;/strong&gt; - e.g: 200, 400, 401, 504 Gateway Timeout etc (we aim for a &lt;code&gt;200&lt;/code&gt; status code, since it tells us everything went ok and the request is successful)&lt;br&gt;
&lt;strong&gt;Response header fields&lt;/strong&gt; - hold additional information about the response, like its location or about the server providing it.&lt;/p&gt;

&lt;p&gt;An example of an &lt;code&gt;HTML&lt;/code&gt; document can look something like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="cp"&gt;&amp;lt;!doctype HTML&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA-Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"ie=edge"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;This is my page&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"styles.css"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"mainScripts.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"heading"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;This is my page&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;A paragraph with a &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://example.com/about"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;link&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"myImage.jpg"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"image description"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"sideEffectsScripts.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;For the same google search I mentioned earlier, this is how the &lt;code&gt;reponse headers&lt;/code&gt; look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ekbshftg82362sbalaj.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%2F2ekbshftg82362sbalaj.PNG" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we take a look at the HTML document, we see that it refrences different &lt;code&gt;CSS&lt;/code&gt; and &lt;code&gt;Javascript&lt;/code&gt; files. These files will no be requested until the browser won't encounter these links but this doesn't happen in this step, but in the &lt;code&gt;parsing&lt;/code&gt; phase which we'll discuss in the next articles. At this point in time, only the HTML is requested and received from the server.&lt;/p&gt;

&lt;p&gt;The response for this initial request contains the first byte of data received. &lt;code&gt;Time to First Byte&lt;/code&gt; (TTFB) is the time between when the user made the request (by typing the website's name in the address bar) and the receipt of the first packet of HTML (which is usually 14kb). &lt;/p&gt;

&lt;h3&gt;
  
  
  TCP Slow Start and congestion algorithms
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;TCP slow start&lt;/code&gt; is an algorithm that balances the speed of a network connection. The first data packet will be 14kb (or smaller) and the way it works is that the amount of data transmitted is increased gradually until a predetermined treshold is reached. After each packet of data is received from the server, the client responds with an &lt;code&gt;ACK message&lt;/code&gt;. Since the connection has a limited capacity, if the server sends too many packets too quickly, they will be dropped. The client won't send any &lt;code&gt;ACK messages&lt;/code&gt; so the server will interpret this as congestion. This is where &lt;code&gt;congestion algorithms&lt;/code&gt; come into play. They monitor this flow of sent packets and ACK messages to determine a rate at which traffic is optimal and create a steady traffic stream.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Refrence materials:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/Performance/How_browsers_work#navigation" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Fetching_data" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.keycdn.com/support/tcp-slow-start#:~:text=TCP%20slow%20start%20is%20part,thus%20resulting%20in%20network%20congestion." rel="noopener noreferrer"&gt;KeyCDN&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
