<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arika O</title>
    <description>The latest articles on DEV Community by Arika O (@arikaturika).</description>
    <link>https://dev.to/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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F433146%2Fdbb84bca-03d0-4b56-9627-3ac7b3e2c857.png</url>
      <title>DEV Community: Arika O</title>
      <link>https://dev.to/arikaturika</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arikaturika"/>
    <language>en</language>
    <item>
      <title>Writing in the Age of AI</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Sun, 07 Jun 2026 21:45:06 +0000</pubDate>
      <link>https://dev.to/arikaturika/writing-in-the-age-of-ai-3ib</link>
      <guid>https://dev.to/arikaturika/writing-in-the-age-of-ai-3ib</guid>
      <description>&lt;p&gt;I haven't written articles in quite a while and I recently decided to come back to it. &lt;/p&gt;

&lt;p&gt;At work I use AI daily, so a big part of my coding tasks are delegated to agents. I try not to do the same when it comes to writing text but I don't have a clear reason for that (or maybe I do and I don't want to admit it).&lt;/p&gt;

&lt;p&gt;I am sure people can take advantage of generating text with the help of AI but at the moment I feel like prompting would not save me any time and writing the text myself would be faster. &lt;/p&gt;

&lt;p&gt;What about you? Are you using AI to write your articles?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pexels.com/@jessica-olivella-555697728/" rel="noopener noreferrer"&gt;Image credit - jessica olivella, on pexels&lt;/a&gt;&lt;/p&gt;

</description>
      <category>writing</category>
      <category>ai</category>
      <category>programming</category>
      <category>discuss</category>
    </item>
    <item>
      <title>React useEffect with extra dependecies?</title>
      <dc:creator>Arika O</dc:creator>
      <pubDate>Sun, 07 Jun 2026 17:10:56 +0000</pubDate>
      <link>https://dev.to/arikaturika/code-smell-or-not-useeffect-with-extra-dependecies-4a05</link>
      <guid>https://dev.to/arikaturika/code-smell-or-not-useeffect-with-extra-dependecies-4a05</guid>
      <description>&lt;p&gt;I recently came across a piece of React code that caught my attention (new project, new codebase and this pattern is used quite a lot).&lt;/p&gt;

&lt;p&gt;Imagine you have a table displaying a list of movies and you can select each movie row by ticking a checkbox. The checkbox selection needs to reset every time the user selects a different year, category or rating somewhere else on the page (the table needs to display new data when these filters are changed, so the selection is no longer valid).&lt;/p&gt;

&lt;p&gt;The code I am talking about was a useEffect that was setting the &lt;code&gt;selectedMovieId&lt;/code&gt; state and it was written something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;selectedMovieId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSelectedMovieId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// more code here...&lt;/span&gt;

&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nf"&gt;setSelectedMovieId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;span class="nx"&gt;selectedYear&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nx"&gt;selectedCategory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nx"&gt;selectedUserRating&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I looked at the code for a few seconds before understanding that the developer was trying to update the state by providing extra dependencies to useEffect instead of doing that inside the event handlers of the year, category and user rating filters. &lt;/p&gt;

&lt;p&gt;I would have written the code differently, making the &lt;code&gt;selectedMovieId&lt;/code&gt; value get changed by functions, which themselves would be the source of the mutation and remove the useEffect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleYearChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;YearType&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="nf"&gt;setSelectedYear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;// something something...&lt;/span&gt;
  &lt;span class="nf"&gt;setSelectedMovieId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleCategoryChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;CategoryType&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="nf"&gt;setSelectedCategory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;category&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;// something something...&lt;/span&gt;
  &lt;span class="nf"&gt;setSelectedMovieId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleRatingChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RatingType&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="nf"&gt;setSelectedUserRating&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;// something something...&lt;/span&gt;
  &lt;span class="nf"&gt;setSelectedMovieId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the original code is working, if in the future more filters will be added, we risk that the useEffect will never be updated with the extra dependencies, since this piece of logic can be easily missed. &lt;/p&gt;

&lt;p&gt;Would you use this pattern? If yes, when?&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <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://dev.to/arikaturika/tree-shaking-in-javascript-short-explanation-3ci4</link>
      <guid>https://dev.to/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://dev.to/arikaturika/one-concept-a-day-early-return-pattern-in-javascript-3pol</link>
      <guid>https://dev.to/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://dev.to/arikaturika/manipulating-the-dom-using-javascript-traversing-the-dompart-2-o7</link>
      <guid>https://dev.to/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://dev.to/arikaturika/remember-to-whitelist-your-ip-when-you-cant-connect-to-mongo-db-1pgn</link>
      <guid>https://dev.to/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://dev.to/arikaturika/how-web-browsers-work-the-render-tree-part-7-with-illustrations-24h3</link>
      <guid>https://dev.to/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://dev.to/arikaturika/what-books-are-you-reading-these-days-and-we-should-too-47b8</link>
      <guid>https://dev.to/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://dev.to/arikaturika/manipulating-the-dom-using-javascript-how-to-select-nodes-part-1-38j</link>
      <guid>https://dev.to/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://dev.to/arikaturika/how-web-browsers-work-creating-the-accessibility-tree-part-6-with-illustrations-2hl2</link>
      <guid>https://dev.to/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://dev.to/arikaturika/how-web-browsers-work-executing-the-javascript-part-5-with-illustrations-21ok</link>
      <guid>https://dev.to/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://dev.to/arikaturika/how-web-browsers-work-parsing-the-css-part-4-with-illustrations-4c</link>
      <guid>https://dev.to/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>
  </channel>
</rss>
