<?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: Ranjeet Singh</title>
    <description>The latest articles on DEV Community by Ranjeet Singh (@jeetsdev).</description>
    <link>https://dev.to/jeetsdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F667132%2F1a5381f4-eae8-400e-a6c8-8a4abd3e7971.png</url>
      <title>DEV Community: Ranjeet Singh</title>
      <link>https://dev.to/jeetsdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jeetsdev"/>
    <language>en</language>
    <item>
      <title>Event Bubbling and Capturing in JavaScript</title>
      <dc:creator>Ranjeet Singh</dc:creator>
      <pubDate>Sat, 06 Aug 2022 12:32:00 +0000</pubDate>
      <link>https://dev.to/jeetsdev/event-bubbling-and-capturing-in-javascript-3ifd</link>
      <guid>https://dev.to/jeetsdev/event-bubbling-and-capturing-in-javascript-3ifd</guid>
      <description>&lt;p&gt;In JavaScript, you must have worked with events. In this article, we are going to learn two concepts of events which is Event Bubbling and Event Capturing. Both of these concepts are related to Event Propagation which is also known as Event Flow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;To understand these concepts let's understand what's an event first. As per &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;, definition Events are actions or occurrences that happen in the system you are programming — the system produces (or "fires") a signal of some kind when an event occurs, and provides a mechanism by which an action can be automatically taken (that is, some code running) when the event occurs. You can consider it like you giving a signal to your friend to do something so when you give that signal then your friend starts doing the thing.&lt;/p&gt;

&lt;p&gt;There are two main event models: Event Bubbling and Event Capturing.&lt;/p&gt;

&lt;p&gt;Now let's understand them one by one-&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Bubbling
&lt;/h2&gt;

&lt;p&gt;Event Bubbling is the concept of invoking the event handlers where the event handlers are nested in another element. Bubbling happens when an event is actually fired on an element, it will first handle the event on itself, then the event on its parent(if any) and continue till it reaches the body and the root element.&lt;/p&gt;

&lt;p&gt;Let's look at an example to understand it - &lt;/p&gt;

&lt;p&gt;This is our HTML 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="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"grandparent"&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;id=&lt;/span&gt;&lt;span class="s"&gt;"parent"&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;id=&lt;/span&gt;&lt;span class="s"&gt;"child"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and here we have added the event listeners on them :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;       &lt;span class="nx"&gt;grandparent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Grandparent clicked.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;parent clicked.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child clicked.&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we click on the child element we will get an output similar to this : &lt;/p&gt;

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

&lt;p&gt;As we can see here the event is getting triggered from child to parent to grandparent so it means in the Bubbling model event is getting triggered from the innermost element covering all its ancestors along the way. This is just like the bubbles which rise from the bottom to the top in a carbonated drink, hence the name bubbling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Capturing
&lt;/h2&gt;

&lt;p&gt;This is the exact opposite of Bubbling. In the Event Capturing model, an event starts at the least specific element and flows downward toward the most specific element. It means the event will get triggered from grandparent to parent to child.&lt;/p&gt;

&lt;p&gt;To use the capturing model you have to pass the &lt;code&gt;true&lt;/code&gt; at the place of the third argument in the &lt;code&gt;addEventListner&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Here is the JS code snippet :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;        &lt;span class="nx"&gt;grandparent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Grandparent clicked.&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="kc"&gt;true&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="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;parent clicked.&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="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;child clicked.&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="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;You can understand both the models by the following image too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fka3nkx3mjv33qwoj37gp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fka3nkx3mjv33qwoj37gp.png" alt="event-bubbling-and-capturing-in-javascript" width="500" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is all that I have in my mind, for now, I will add more in the future. Thanks for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Event loop and Call stack in JavaScript</title>
      <dc:creator>Ranjeet Singh</dc:creator>
      <pubDate>Fri, 22 Jul 2022 09:28:00 +0000</pubDate>
      <link>https://dev.to/jeetsdev/event-loop-and-call-stack-in-javascript-32hk</link>
      <guid>https://dev.to/jeetsdev/event-loop-and-call-stack-in-javascript-32hk</guid>
      <description>&lt;p&gt;Event Loop is a confusing enough topic to beginners and is often misunderstood.  Here I am going to explain some terms so that you can better understand how things work and what the Event loop is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;We hear all the time that JavaScript is a single-threaded programming language which means JavaScript can execute one thing at a time. JavaScript engine executes one line at a time from the top of the file synchronously.  So does it means If a function takes a long time to execute, you cannot interact with the web browser during the function’s execution because the page hangs?&lt;/p&gt;

&lt;p&gt;The web browser also has other components, not just the JavaScript engine.&lt;/p&gt;

&lt;p&gt;When you make a fetch request, call the setTimeout() function, or click a button, the web browser can do these activities concurrently and synchronously.&lt;/p&gt;

&lt;p&gt;The setTimeout(), fetch requests, and DOM events are parts of the Web APIs of the web browser. These Web APIs are not part of JavaScript. They are part of Browser.&lt;/p&gt;

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

&lt;p&gt;Let's understand each part individually so that we can get an idea of how things work asynchronously.&lt;/p&gt;

&lt;h2&gt;
  
  
  Call Stack and Heap
&lt;/h2&gt;

&lt;p&gt;As we can see here JavaScript engine has a memory heap and a Call Stack. The memory Heap allocates memory for functions and variables and updates the values as per the logic. &lt;br&gt;
The Call Stack is present inside the JavaScript engine and it can execute one thing at a time and all the code gets executed inside this Call Stack. Call Stack follows the Last-In-First-Out (LIFO) sequence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web API
&lt;/h2&gt;

&lt;p&gt;The Web APIs are provided by the web browser which gives additional functionality. The Web API calls are added to the Web API container from the Call Stack. So let's suppose we call a set timeout function then &lt;br&gt;
these results don't instantly get inserted into the Call Stack because that would disturb the flow of execution. Rather, it gets added at the end of the Task Queue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Task Queue
&lt;/h2&gt;

&lt;p&gt;This is a queue that holds all of the tasks which come from the Web API. Task Queue follows the First-In-First-Out (FIFO) sequence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Loop
&lt;/h2&gt;

&lt;p&gt;The Event Loop is essentially an infinite while loop that continuously runs and checks for two things-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the Call Stack is empty.&lt;/li&gt;
&lt;li&gt;If there is any task in Task Queue.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When both these conditions become true, the event loop picks up the first element in the queue and puts it on the call stack for the main thread to execute it.&lt;/p&gt;

&lt;p&gt;Thanks for reading! Hope that this makes you feel a bit more comfortable with the event loop!&lt;br&gt;
Feel free to reach out to me if you have any questions on &lt;a href="https://twitter.com/jeetsdev" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; 😊.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Async VS Defer - Understand The JavaScript Execution</title>
      <dc:creator>Ranjeet Singh</dc:creator>
      <pubDate>Sat, 14 Aug 2021 09:32:03 +0000</pubDate>
      <link>https://dev.to/jeetsdev/async-vs-defer-understand-the-javascript-execution-228g</link>
      <guid>https://dev.to/jeetsdev/async-vs-defer-understand-the-javascript-execution-228g</guid>
      <description>&lt;p&gt;As a web developer, how many times you have written this line in your code?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Probably too many times to count, but do you really understand how the browser handles this simple line of code? We have always been advised to put our &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag at the end of the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tag, but again why is that so...? Is there any other place where we can put our &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag? What if we put the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag inside the &lt;code&gt;&amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;&lt;/code&gt; tag of our HTML document like -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- inside the head tag --&amp;gt;
&amp;lt;head&amp;gt;
    ...
    &amp;lt;title&amp;gt; .... &amp;lt;/title&amp;gt;
    &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ejckr03kzs86u2xwace.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ejckr03kzs86u2xwace.gif" alt="Gif here" width="500" height="303"&gt;&lt;/a&gt;&lt;br&gt;
Aahhh, enough of these questions. Now it's time to understand all of them.&lt;/p&gt;

&lt;p&gt;First and foremost, Yes we can put our &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag wherever we want, but remember one thing that it can affect your page performance. &lt;/p&gt;

&lt;p&gt;So now let's understand exactly how &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag loading works and most importantly how we can use &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; to speed up our Javascript loading and improve our page performance. &lt;/p&gt;
&lt;h2&gt;
  
  
  How Browser Parse HTML
&lt;/h2&gt;

&lt;p&gt;Before understanding how &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; is loading, we first need to understand how the browser parses HTML. Luckily, it's pretty straightforward. The browser will parse HTML from the top of the document to the bottom, and when it hits a resource, like an &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag it will send out a request for that resource and continue parsing. The important thing to note is that the browser does not stop parsing the HTML to get the &lt;code&gt;img src&lt;/code&gt;. This is why when you load a web page you may notice the page jumps around as the images pop in since they are loaded in the background and may finish downloading after the HTML is parsed.&lt;/p&gt;

&lt;p&gt;But that's not the case with the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag. When the browser comes across a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag when loading HTML, the browser is forced to download and parse the entire &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; and evaluate it first, before it can continue with reading the rest of the HTML to build the DOM. This is why we are advised to put our &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag at the bottom of our HTML body so they don't delay the parsing of the HTML.&lt;/p&gt;

&lt;p&gt;Now you can just think that putting the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag at the bottom of the HTML body is ideal, but what if the HTML is too large and it takes some time to get downloaded and parsed then the JavaScript will not start downloading until all of the HTML is parsed which could delay your &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; download and affect your page performance. This is why the &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; attributes were created.&lt;/p&gt;
&lt;h2&gt;
  
  
  Async and Defer
&lt;/h2&gt;

&lt;p&gt;Both &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; attributes load the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; without blocking the DOM and make the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag work like as a &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag to the parser as described above. This means that the script will be fetched in the background and continue parsing as normal without waiting. Okay, that seems fair but what's the difference between them...? Here we go then.&lt;/p&gt;
&lt;h2&gt;
  
  
  Async vs Defer
&lt;/h2&gt;

&lt;p&gt;Both &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; look like they do the same thing at the first glance, but that's not the case, there is a subtle difference between the two.&lt;/p&gt;
&lt;h3&gt;
  
  
  Defer waits for the DOM but Async doesn't -
&lt;/h3&gt;

&lt;p&gt;The first and most important difference Is &lt;code&gt;async&lt;/code&gt; doesn't care whether DOM is fully loaded or not, on the other side &lt;code&gt;defer&lt;/code&gt; waits for the DOM to get loaded first and after that, it starts execution of the scripts.&lt;/p&gt;

&lt;p&gt;For example, let's say you have 25000 buttons in your HTML document and now select every button of the DOM using both of the scripts and get the length of them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
  &amp;lt;script src="defer.js" defer&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;script src="async.js" async&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;div class="container"&amp;gt;
  &amp;lt;!-- 25000 buttons --&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Here are our script codes...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//! Async script code here
let asyncButton = document.querySelectorAll('button');
console.log(`Async script button count: ${asyncButton.length}`);

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Defer script code here
let deferButton = document.querySelectorAll('button');
console.log(`Defer script button count: ${deferButton.length}`);

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

&lt;/div&gt;



&lt;p&gt;And here is the console output...&lt;/p&gt;

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

&lt;p&gt;As you can see now, &lt;code&gt;async&lt;/code&gt; is not waiting for the DOM to get loaded fully and selecting all the buttons loaded at the time of execution of the script and on the other hand, &lt;code&gt;defer&lt;/code&gt; is waiting for the DOM elements to get loaded first and that's why it's selecting every button presented at the DOM.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If your script is dependent on the DOM, then never ever use the &lt;code&gt;async&lt;/code&gt; attribute, there's the possibility that the element you need get undefined and this is a potential source of bugs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Defer maintain the order of JS files Async doesn't -
&lt;/h3&gt;

&lt;p&gt;What does it mean though, take another example to understand it? Let's say you will have four scripts. Each script logs the number of that script. Now if we gonna use the &lt;code&gt;async&lt;/code&gt; attribute in the scripts, the order of executing scripts become unpredictable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;script src="one.js" async&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="two.js" async&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="three.js" async&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="four.js" async&amp;gt;&amp;lt;/script&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;The console output will be something like this...&lt;/p&gt;

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

&lt;p&gt;But what if we use the &lt;code&gt;defer&lt;/code&gt; attribute in every script?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;script src="one.js" defer&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="two.js" defer&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="three.js" defer&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="four.js" defer&amp;gt;&amp;lt;/script&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;And here is the output...&lt;/p&gt;

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

&lt;p&gt;So now you can see clearly that &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;defer&lt;/code&gt; always maintain the order of the script so if you have scripts that depend on each other then always consider using &lt;code&gt;defer&lt;/code&gt; rather than &lt;code&gt;async&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;ul&gt;
&lt;li&gt;Adding the &lt;code&gt;defer&lt;/code&gt; attribute will make sure DOM gets loaded first and then the scripts get executes in the given order.&lt;/li&gt;
&lt;li&gt;Adding the &lt;code&gt;async&lt;/code&gt; attribute will execute the script as soon as it gets loaded and this will not follow any order. So avoid using it if your script is dependent on each other or DOM.&lt;/li&gt;
&lt;li&gt;So practically, &lt;code&gt;defer&lt;/code&gt; is more useful than &lt;code&gt;async&lt;/code&gt;, and most of the time you would want &lt;code&gt;defer&lt;/code&gt; instead of &lt;code&gt;async&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; is great when you want to load the script in the middle.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all about &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; and script loading in JavaScript. If you enjoyed this article, please tell a friend about it or share it on your social media handles and make sure you comment below and share your thoughts about it. Thank you.🙏&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/jeetsdev" rel="noopener noreferrer"&gt;Follow me on Twitter&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>HTML Semantic Tags, Why is it important to use Semantic HTML?</title>
      <dc:creator>Ranjeet Singh</dc:creator>
      <pubDate>Wed, 14 Jul 2021 16:27:17 +0000</pubDate>
      <link>https://dev.to/jeetsdev/html-semantic-tags-why-is-it-important-to-use-semantic-html-32ek</link>
      <guid>https://dev.to/jeetsdev/html-semantic-tags-why-is-it-important-to-use-semantic-html-32ek</guid>
      <description>&lt;p&gt;Often times I come across this jargon word &lt;strong&gt;Semantic&lt;/strong&gt; in HTML, Here I am gonna explain this &lt;strong&gt;Semantic&lt;/strong&gt; word and also what it is used for in HTML, So first and for most let's understand the meaning of &lt;strong&gt;Semantic&lt;/strong&gt; word, As Wikipedia says, &lt;strong&gt;Semantic&lt;/strong&gt; means syntactically valid string, so we can say that &lt;strong&gt;Semantic&lt;/strong&gt; tags mean syntactically valid tags or in simple words, we can say that &lt;strong&gt;Semantic&lt;/strong&gt; elements are those elements that convey some specific meanings and indicate what type of content they contain and what role that content plays in the document.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Why do we need to use semantic HTML tags?
&lt;/h3&gt;

&lt;p&gt;But why bother writing &lt;strong&gt;Semantic&lt;/strong&gt; HTML, If you design all the &lt;code&gt;div&lt;/code&gt; correctly then you won't see any difference in the UI of your webpage. However, remember one thing that you are not just dealing with humans, there are  &lt;a href="https://en.wikipedia.org/wiki/Web_crawler" rel="noopener noreferrer"&gt;Web Crawlers&lt;/a&gt; and search engines spiders too, they read web pages automatically, without prior knowledge of what they might find. These are dependent on the &lt;strong&gt;Semantic&lt;/strong&gt; clarity of web pages they find as they use various techniques and algorithms to read and index millions of web pages a day and provide web users with relevant results and obviously, leads to a better search engine ranking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Semantic&lt;/strong&gt; elements also increase the  &lt;a href="https://en.wikipedia.org/wiki/Web_Content_Accessibility_Guidelines" rel="noopener noreferrer"&gt;web accessibility&lt;/a&gt;  of the web pages and help screen readers and as we know the number of smart devices keeps growing. It also simplifies page navigation for assistive technologies as well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Semantic&lt;/strong&gt; elements also improve the code structure and make code more readable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some Semantic HTML tags
&lt;/h3&gt;

&lt;p&gt;There is pleather of &lt;strong&gt;Semantic&lt;/strong&gt; tags that exist in HTML but most of them are not that much used on most websites. Some &lt;strong&gt;Semantic&lt;/strong&gt; tags that are mostly used and popular too are : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Header&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nav&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;section&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;aside&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;figure&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;figcaption&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;article&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;details&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;footer&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can learn about roughly 100 Semantic tags examples on  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element" rel="noopener noreferrer"&gt;MDN&lt;/a&gt; or &lt;a href="https://html.spec.whatwg.org/" rel="noopener noreferrer"&gt;HTML Standard&lt;/a&gt;, and yeah if you want a detailed article on these tags and how they work internally then let me know and I will surely come up with one more article on that too.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Semantic&lt;/strong&gt; HTML is not only about using the new &lt;strong&gt;Semantic&lt;/strong&gt; tags but about using the correct tags for each element so that it is easy to navigate for all users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the end, I just want to say that learning &lt;strong&gt;Semantic&lt;/strong&gt; markup will make your life easier, your code cleaner, and your website better. &lt;/p&gt;

&lt;p&gt;Thanks for reading 🙏 &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>html</category>
    </item>
  </channel>
</rss>
