<?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: Shubh Sharma</title>
    <description>The latest articles on DEV Community by Shubh Sharma (@shubhsharma19).</description>
    <link>https://dev.to/shubhsharma19</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%2F738861%2F35da1226-8470-424f-93a0-7d1b18b0b4bf.jpg</url>
      <title>DEV Community: Shubh Sharma</title>
      <link>https://dev.to/shubhsharma19</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubhsharma19"/>
    <language>en</language>
    <item>
      <title>How I Pivoted to Design (2025 Wrapped)</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Mon, 29 Dec 2025 08:46:18 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/how-i-pivoted-to-design-2025-wrapped-225</link>
      <guid>https://dev.to/shubhsharma19/how-i-pivoted-to-design-2025-wrapped-225</guid>
      <description>&lt;h2&gt;
  
  
  TLDR
&lt;/h2&gt;

&lt;p&gt;I found what I was good at in 2025, and then focused fully on it. Got a full-time job and completed 28 projects in 6months as a UI/UX Designer.&lt;/p&gt;

&lt;h2&gt;
  
  
  2024 Recap and 2025 Starting
&lt;/h2&gt;

&lt;p&gt;I started my journey in 2023 as a CS student who wanted to be good Full-stack developer. Later in 2024 I continued this journey while also doing some designing as a hobby, Those who know me from starting knows that I used to design a lot of stuff for my Instagram page. I never realised that designing was a skill I was naturally good at.&lt;/p&gt;

&lt;p&gt;2024 was all about my first internship opportunity at &lt;strong&gt;Kidjig Tech&lt;/strong&gt; as a &lt;strong&gt;UIUX designer&lt;/strong&gt; (May 2024 to October 2024) and I learned a lot in that internship opportunity. I worked as an intern for 6 months and then in last 3 months of 2024 I did freelancing as it was giving me more freedom and better pay as well.&lt;/p&gt;

&lt;p&gt;January 2025, Started the year with my &lt;strong&gt;3rd Freelance design money&lt;/strong&gt;. I designed mobile apps and Website design for &lt;a href="https://www.youtube.com/@TechTutorialswithPiyush" rel="noopener noreferrer"&gt;Piyush Sachdeva&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I was making around &lt;strong&gt;100-150 USD&lt;/strong&gt; per project at that time but I wasn't serious about design and was just doing it as a side hobby or a side thing as everyone was doing tech and coding stuff and were making really good money.&lt;/p&gt;

&lt;h2&gt;
  
  
  Feb to April
&lt;/h2&gt;

&lt;p&gt;Completed my third semester exams of MCA and joined Kidjig again as a full-time &lt;strong&gt;Designer&lt;/strong&gt;. Designed lots of apps and stuff there. Kidjig gave me a lot of room to learn and expand my skills in those 3 months.&lt;/p&gt;

&lt;h2&gt;
  
  
  A way forward
&lt;/h2&gt;

&lt;p&gt;Left Kidjig to seek better opportunities, Gave 3 interviews with 100% Success rate in early stage startups but the pay wasn't that good so I continued freelancing. ($120-150 per project)&lt;/p&gt;

&lt;h2&gt;
  
  
  Path to something better
&lt;/h2&gt;

&lt;p&gt;I gave my 4th semester exams (last semester) in May. Full month was gone in just giving stupid exams in college and overthinking about my future, but finally completed my exams. &lt;/p&gt;

&lt;p&gt;After last exam I started reaching out to some of my connections on Twitter to connect with them and find some opportunities and then Hrithik (was already working there) connected me with an agency. &lt;/p&gt;

&lt;p&gt;So In June I started doing some projects and earned around $350 per project (&lt;strong&gt;Clients loved those designs and the pay was good&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;This was the turning point for me as I was enjoying what i was doing and also getting paid good. This is when I realised I could do designing as a full-time thing and make good money.&lt;/p&gt;

&lt;p&gt;I was given a full-time offer from the agency and I signed the contract and NDA. &lt;/p&gt;

&lt;h2&gt;
  
  
  Pivot to design (July to Present)
&lt;/h2&gt;

&lt;p&gt;This was the turning point for me financially and for my career as well. I stopped coding and started focusing on UI/UX design skills only. I started reading UIUX books, principles and more. I already knew wire-framing, prototyping, etc so this was a refresher on those skills.&lt;/p&gt;

&lt;p&gt;In September and October around Diwali I was able to afford &lt;strong&gt;Macbook Air and iPhone&lt;/strong&gt; from my own earned money, this was an investment as my old devices were already in a very bad situation.&lt;/p&gt;

&lt;p&gt;Tbh I cant explain in words how grateful I am today for this opportunity, I have worked very hard in last 6 months and completed total 28 Projects including Mobile apps, Landing pages, Web apps and also social media ads design. I have worked 8-10 hours average and even on weekends to complete deadlines.&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%2F4s7fjdx3oeqyuscsf4tu.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%2F4s7fjdx3oeqyuscsf4tu.png" alt="My current setup" width="800" height="791"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Some bad things that happened
&lt;/h2&gt;

&lt;p&gt;My mother's health wasn't good this year but i tried my best to help her and provide her best treatment for her health. I was able to afford good healthcare for her without too much thinking.&lt;/p&gt;

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

&lt;p&gt;I am writing this blog today on 29th December 2025 and tbh i am not sure what future awaits for me, as humans we can only hope for better but somethings that I have in mind which i want to do in 2026. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Want to travel with my friends and family&lt;/li&gt;
&lt;li&gt;Focus more on health of myself and my family&lt;/li&gt;
&lt;li&gt;Improve on my design skills even further&lt;/li&gt;
&lt;li&gt;Buy my own bike&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I still remember when i used to overthink at night what will be my future but now I just think about today. Thats all I have in mind for now. &lt;/p&gt;

&lt;p&gt;Thankyou God, Figma and Internet. I will see you in 2026. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>design</category>
    </item>
    <item>
      <title>Best JavaScript One Liners that You Must Know</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Tue, 21 May 2024 15:37:41 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/best-javascript-one-liners-that-you-must-know-3740</link>
      <guid>https://dev.to/shubhsharma19/best-javascript-one-liners-that-you-must-know-3740</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;JavaScript offers powerful techniques that can be used to to do very complex tasks in one line, these can be called one-liners. These are short lines of code that can perform complex tasks, making your code compact.&lt;/p&gt;

&lt;p&gt;Especially while working with react I am using these one liners a lot.&lt;/p&gt;

&lt;p&gt;So, Let's Start! 🚀🚀🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Ternary Operators Instead of If/Else
&lt;/h2&gt;

&lt;p&gt;Instead of using &lt;code&gt;If/else&lt;/code&gt; you can use &lt;code&gt;ternary operators&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Condition is described before question mark &lt;code&gt;?&lt;/code&gt; and then we put true value and then &lt;code&gt;:&lt;/code&gt; colon and then false value.&lt;/p&gt;

&lt;p&gt;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;let&lt;/span&gt; &lt;span class="nx"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// if condition is true then hello is assigned to variable&lt;/span&gt;
&lt;span class="c1"&gt;// otherwise bye is assigned&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;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;23&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;isAdult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You're an adult&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You're not an adult&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;isAdult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can even use it for &lt;code&gt;if / else if / else&lt;/code&gt; conditions making more chained and complex decisions in easier and shorter code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// example&lt;/span&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;23&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;isAdult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;  &lt;span class="c1"&gt;// if &lt;/span&gt;
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You are a teenager!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;// assign this &lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;  &lt;span class="c1"&gt;// else if &lt;/span&gt;
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You are over 18!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;// assign this &lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="c1"&gt;// else if &lt;/span&gt;
    &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You are old!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// assign this &lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You are a kid!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// else&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;isAdult&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 very much used in professional code when we want to execute something on the basis of some given conditions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Printing values of an array
&lt;/h2&gt;

&lt;p&gt;Instead of using a for loop we can use &lt;code&gt;forEach()&lt;/code&gt; method with implicit return values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;code&gt;num&lt;/code&gt; is each element of the array being printed on console.&lt;/p&gt;

&lt;h2&gt;
  
  
  Filtering out values from an array
&lt;/h2&gt;

&lt;p&gt;In a single line of code you can use &lt;code&gt;filter()&lt;/code&gt; method to filter out values of an array.&lt;/p&gt;

&lt;p&gt;Filter method creates a new array of filtered out values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filteredNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="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;filteredNums&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// output: 2, 4, 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Finding if a value exist or not in an array
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isElementFound&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Yes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No&lt;/span&gt;&lt;span class="dl"&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;isElementFound&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// output: No&lt;/span&gt;
&lt;span class="c1"&gt;// includes() returns boolean value: true/false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This above code assigns No in the variable since there is no 19 value in the nums array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Transforming an array using map
&lt;/h2&gt;

&lt;p&gt;Map method can be used to transform existing values of an array and then return those values in form of new array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&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;squaredNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num&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;squaredNums&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// output: 1, 4, 9, 16&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;"map() and filter() methods are the most used methods in react"&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting the length of an array or string
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shubh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringLen&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="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;stringLen&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//output: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Getting values out of an object
&lt;/h2&gt;

&lt;p&gt;We use &lt;strong&gt;destructuring&lt;/strong&gt; syntax to get out those values from the object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="na"&gt;w&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="na"&gt;x&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="na"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="na"&gt;nestedObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="na"&gt;z&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;nestedObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;object&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;w&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;x&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;y&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;z&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// output: 0 1 2 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Getting values out of arrays
&lt;/h2&gt;

&lt;p&gt;We use destructuring syntax to get out those values from arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;nestedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;first&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;nested1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nested2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nestedArray&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;first&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nested1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nested2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Output: 4&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;last&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// output: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you can see that we didnt print 2 because we skipped it, yes we actually skipped it, look here ➡️ &lt;code&gt;[first, ,&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This extra comma means &lt;code&gt;skip the value&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Swapping values using destructuring
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;a&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;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// output: 20&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// output: 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation: "put value of b in a and value of a in b" ➡️ &lt;code&gt;[a, b] = [b, a];&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Spread Operator to merge, split and manipulate arrays and strings
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&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;mergedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;array1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;array2&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;mergedArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// output: 1,2,3,4,5,6,7,8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// spliting values of a string using spread operator&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stringArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shubh&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;stringArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// output: [ 'S', 'h', 'u', 'b', 'h' ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// rest operator&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;secondValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;restValues&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="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;restValues&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// output: [ 3, 4, 5 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Finding min and max values of an array using Spread operator
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&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;minNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// minNumber will be 1&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;maxNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// maxNumber will be 8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Default function parameters
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&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;World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;        &lt;span class="c1"&gt;// Output "Hello, World!"&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shubh&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output "Hello, Shubh!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more upcoming content on &lt;strong&gt;JavaScript, React, and other Web Development topics&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For paid collaboration, you can mail me at: &lt;a href="//mail.shubhsharma19@gmail.com"&gt;gmail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="//twitter.com/shubhstwt"&gt;Twitter&lt;/a&gt;, &lt;a href="//linkedin.com/in/shubhsharma19"&gt;LinkedIn&lt;/a&gt; and &lt;a href="//github.com/shubhxg"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thank you for Reading&lt;/strong&gt; :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Self taught developers should avoid these traps</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Sun, 21 Apr 2024 08:35:57 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/self-taught-developers-should-avoid-these-traps-1fgi</link>
      <guid>https://dev.to/shubhsharma19/self-taught-developers-should-avoid-these-traps-1fgi</guid>
      <description>&lt;h2&gt;
  
  
  1. Tutorial hell trap
&lt;/h2&gt;

&lt;p&gt;I think this was very obvious but still wanted to add this as well in this list.&lt;/p&gt;

&lt;p&gt;When you will be learning to code there will be a phase of your learning journey when you will get stuck in this for atleast once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tutorial hell&lt;/strong&gt; is basically the feeling you get that you will not be able to make a project or code unless you follow a tutorial. &lt;/p&gt;

&lt;p&gt;The real question is &lt;strong&gt;"How to get out of tutorial hell?"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer is very simple:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Learn the &lt;strong&gt;fundamental basics&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Start &lt;strong&gt;building&lt;/strong&gt; some basic project very very basic from beginning (could be anything)&lt;/li&gt;
&lt;li&gt;Got stuck? Watch the tutorial if help needed or &lt;strong&gt;google it&lt;/strong&gt;, you also have &lt;strong&gt;AI tools&lt;/strong&gt; to help you in 2024&lt;/li&gt;
&lt;li&gt;Find the &lt;strong&gt;solution&lt;/strong&gt; and &lt;strong&gt;improve&lt;/strong&gt; your learnings&lt;/li&gt;
&lt;li&gt;Do not be scared of making &lt;strong&gt;mistakes&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The only way is to follow these steps and keep building.&lt;/p&gt;

&lt;p&gt;By building projects with minimum required skills you will learn the actual mindset of how developers and engineers build and solve problems by finding solutions themselves. After all they are problem solvers.&lt;/p&gt;

&lt;p&gt;You will feel confident too :)&lt;/p&gt;

&lt;h2&gt;
  
  
  2. CSS Trap
&lt;/h2&gt;

&lt;p&gt;I wasted my 3-4 months just in CSS, yep, I made some projects using pure html and css like &lt;code&gt;twitter clone&lt;/code&gt;, &lt;code&gt;tech crunch clone&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;Then I learnt about `sass and wasted more days to learn it and build more projects.&lt;/p&gt;

&lt;p&gt;Then I learnt &lt;code&gt;bootstrap&lt;/code&gt; and made 2 more basic projects using it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LESSON?????&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While learning &lt;code&gt;CSS&lt;/code&gt; and making projects you will feel like a lot of progress but you should immediately shift to learning Javascript after 1 or 2 projects otherwise you will not do it for a long time and will probably procrastinate like I did.&lt;/p&gt;

&lt;p&gt;I am not saying do not make projects, I am saying once you have learnt css foundations like flexbox, and other stuff you will be good to go ahead for javascript unless you want to be a master in css then its a totally different scenario.&lt;/p&gt;

&lt;p&gt;You have to ask yourself, &lt;strong&gt;do you want to be a frontend/fullstack developer or just a CSS Master?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Comfort trap
&lt;/h2&gt;

&lt;p&gt;When you know you have to learn &lt;strong&gt;javascript&lt;/strong&gt; or &lt;strong&gt;react&lt;/strong&gt; or &lt;strong&gt;nextjs&lt;/strong&gt; or any other technology but you procrastinate it because you are too comfortable in your current technology. &lt;/p&gt;

&lt;p&gt;This might sound controversial but think again, you are just stopping your true growth potential.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;  I used to say &lt;code&gt;Backend&lt;/code&gt; is scary and hard, I will probably go for &lt;code&gt;frontend&lt;/code&gt; but once I learned about nodejs and express for backend I realised it was just my procrastination not letting me explore backend. I enjoyed learning backend. &lt;/p&gt;

&lt;p&gt;And you might enjoy it too once you reach that skill however many beginners won't even reach that point because they will procrastinate it by being too comfortable in their small bubble or will just give up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgjx0l1ymcp26lkdzz5jh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgjx0l1ymcp26lkdzz5jh.jpg" alt="Comfort trap" width="800" height="916"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do not be afraid of anything, its just your mind trying to stay in its comfort bubble, &lt;strong&gt;a trap of mind&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more upcoming content on &lt;strong&gt;JavaScript, React, and other Web Development topics.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For paid collaboration, you can mail me at: &lt;a href="//mail.shubhsharma19@gmail.com"&gt;gmail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="//twitter.com/shubhstwt"&gt;Twitter&lt;/a&gt;, &lt;a href="//linkedin.com/in/shubhsharma19"&gt;LinkedIn&lt;/a&gt; and &lt;a href="//github.com/shubhxg"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for Reading :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why do we need CORS to access CDN links?</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Sat, 20 Apr 2024 14:24:46 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/why-do-we-need-cors-to-access-cdn-links-bh6</link>
      <guid>https://dev.to/shubhsharma19/why-do-we-need-cors-to-access-cdn-links-bh6</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;When you use a &lt;code&gt;Content Delivery Network (CDN)&lt;/code&gt; to host static assets for your website, like &lt;code&gt;images&lt;/code&gt; or &lt;code&gt;Javascript files&lt;/code&gt;, those files are being served from a different domain than your main website.&lt;/p&gt;

&lt;p&gt;This difference in domain can cause security issues with modern web browsers. &lt;/p&gt;

&lt;p&gt;Reason is that browsers hate it when you try to access some resources from a different server than your own website served from.&lt;/p&gt;

&lt;p&gt;This is a policy of browsers called &lt;strong&gt;"Same origin policy"&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What CORS has to do with this
&lt;/h2&gt;

&lt;p&gt;In easy terms, CORS (Cross Origin Resource Sharing) is like a handshake between your website and CDN (could be other servers as well), saying it's okay to load resources from each other. &lt;/p&gt;

&lt;p&gt;It acts as a security check to prevent malicious scripts from loading unauthorized resources.&lt;/p&gt;

&lt;p&gt;If header of request contains a CORS then good ✅👍 &lt;/p&gt;

&lt;p&gt;otherwise bad, gets rejected👎❌&lt;/p&gt;

&lt;p&gt;This meme I made to explain in easy terms:&lt;/p&gt;

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

&lt;p&gt;Thats why you will see something written like &lt;code&gt;crossorigin&lt;/code&gt; in the &lt;code&gt;cdn&lt;/code&gt; links.&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;script 
  &lt;/span&gt;&lt;span class="na"&gt;crossorigin&lt;/span&gt; 
  &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://unpkg.com/react@18/umd/react.development.js"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script 
  &lt;/span&gt;&lt;span class="na"&gt;crossorigin&lt;/span&gt; 
  &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://unpkg.com/react-dom@18/umd/react-dom.development.js"&lt;/span&gt;
&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;CORS help in allowing your website to access resources from different servers.&lt;/p&gt;

&lt;p&gt;If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more upcoming content on JavaScript, React, and other Web Development topics.&lt;/p&gt;

&lt;p&gt;For paid collaboration, you can mail me at: &lt;a href="//mail.shubhsharma19@gmail.com"&gt;gmail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="//twitter.com/shubhstwt"&gt;Twitter&lt;/a&gt;, &lt;a href="//linkedin.com/in/shubhsharma19"&gt;LinkedIn&lt;/a&gt; and &lt;a href="//github.com/shubhxg"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for Reading :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>4 Habits to Avoid Bugs related to Hoisting in your JavaScript Code</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Mon, 15 Apr 2024 19:29:23 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/4-habits-to-avoid-bugs-related-to-hoisting-in-your-javascript-code-4i3i</link>
      <guid>https://dev.to/shubhsharma19/4-habits-to-avoid-bugs-related-to-hoisting-in-your-javascript-code-4i3i</guid>
      <description>&lt;h2&gt;
  
  
  1. Declaring variables at the beginning of the scope
&lt;/h2&gt;

&lt;p&gt;By declaring variables at the beginning of the scope you ensure that these variables are accessible throughout that scope, avoiding any confusion caused by hoisting.&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&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;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;some data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// other code&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. var ❌ let and const ✅
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; instead of &lt;code&gt;var&lt;/code&gt; to declare or initialize variables. &lt;/p&gt;

&lt;p&gt;Variables declared with var are hoisted to the top of their scope  and initialized with &lt;code&gt;undefined&lt;/code&gt;. That's why accessing them before the declaration line will result in undefined.&lt;/p&gt;

&lt;p&gt;On the other hand, variables created with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are also hoisted to their scope, but not &lt;strong&gt;initialized&lt;/strong&gt;. That's why accessing them before the declaration line results in a ReferenceError. &lt;/p&gt;

&lt;p&gt;This behavior is called the &lt;strong&gt;"Temporal Dead Zone" (TDZ)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Also &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are only accessible within the block they are declared in. This eliminates the possibility of issues of hoisting by variables declared with var.&lt;/p&gt;

&lt;p&gt;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="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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined (hoisted, initialized with undefined)&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="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;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: Cannot access 'y' before initialization (not initialized)&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;z&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: Cannot access 'z' before initialization (not initialized)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. "use strict" mode
&lt;/h2&gt;

&lt;p&gt;Enabling strict mode directive at the beginning of your code can prevent some hoisting-related problems.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"use strict";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In strict mode, using an undeclared variable throws an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Use arrow functions and expressions inside a block scope
&lt;/h2&gt;

&lt;p&gt;Using arrow functions and expressions in block scope instead of normal functions.&lt;/p&gt;

&lt;p&gt;Function declarations are hoisted along with their entire definition meaning you can call a function before its declaration in the code.&lt;/p&gt;

&lt;p&gt;Arrow functions and expressions are defined at the point of use, eliminating confusion. Since they are not hoisted they don't create any errors.&lt;/p&gt;

&lt;p&gt;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;function&lt;/span&gt; &lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Calling the function before declaration throws error&lt;/span&gt;
    &lt;span class="c1"&gt;// Error: Cannot access 'myFunc' before initialization&lt;/span&gt;
    &lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Using arrow function&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;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;This is a function expression!&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="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



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

&lt;p&gt;If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more upcoming content on JavaScript, React, and other Web Development topics.&lt;/p&gt;

&lt;p&gt;For paid collaboration, you can mail me at: &lt;a href="//mail.shubhsharma19@gmail.com"&gt;gmail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="//twitter.com/shubhstwt"&gt;Twitter&lt;/a&gt;, &lt;a href="//linkedin.com/in/shubhsharma19"&gt;LinkedIn&lt;/a&gt; and &lt;a href="//github.com/shubhxg"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for Reading :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>DOM Manipulation: Accessing the Elements of the Webpage.</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Sun, 14 Apr 2024 10:39:53 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/dom-manipulation-accessing-the-elements-of-the-webpage-4poe</link>
      <guid>https://dev.to/shubhsharma19/dom-manipulation-accessing-the-elements-of-the-webpage-4poe</guid>
      <description>&lt;h2&gt;
  
  
  Intro ✨
&lt;/h2&gt;

&lt;p&gt;In the previous blog I talked about DOM and how it helps developers in manipulating the values on the web page. This time I will share the guide on how to manipulate the data on the webpage using the JavaScript programming language.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing elements of the webpage 💻
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;em&gt;Browser Environment&lt;/em&gt;&lt;/strong&gt; provides us with bunch of &lt;code&gt;APIs&lt;/code&gt; (methods) to access and manipulate the DOM elements on the page. We can access the elements of the webpage using these methods of the &lt;code&gt;document&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;We can try this all in the chrome developer console, you can access it using the &lt;code&gt;f12&lt;/code&gt; key on the keyboard or &lt;em&gt;right click&lt;/em&gt; on the webpage and click on the &lt;code&gt;inspect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then in the &lt;code&gt;console&lt;/code&gt; tab we can try out our JavaScript code.&lt;/p&gt;

&lt;p&gt;Once we have the hold of the element we can manipulate it easily.&lt;/p&gt;

&lt;p&gt;Here's a list of all the methods we will be using to access elements of the webpage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;getElementByID()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;getElementByClassName()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;querySelector()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;querySelectorAll()&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;getAttribute() and setAttribute()&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  getElementByID() 🆔
&lt;/h3&gt;

&lt;p&gt;This method takes a string &lt;code&gt;id&lt;/code&gt; of the element as input and returns the whole element.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;document.getElementByID('id');&lt;/code&gt;&lt;/p&gt;

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

&lt;h3&gt;
  
  
  getElementByClassName() 👦
&lt;/h3&gt;

&lt;p&gt;This method takes a string &lt;code&gt;class&lt;/code&gt; of the element as input and returns a list of elements.&lt;/p&gt;

&lt;p&gt;This list of the elements is called an &lt;strong&gt;HTMLCollection&lt;/strong&gt;, which is kind of like an array but is not an array.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;document.getElementByClassName('classname')&lt;/code&gt;&lt;/p&gt;

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

&lt;h3&gt;
  
  
  querySelector() ✅
&lt;/h3&gt;

&lt;p&gt;This method is a different one from &lt;code&gt;getElementByID&lt;/code&gt; and &lt;code&gt;getElementByClassName&lt;/code&gt; and the difference is that it can take &lt;code&gt;id&lt;/code&gt; or &lt;code&gt;class&lt;/code&gt; or an &lt;code&gt;element&lt;/code&gt; itself just like we used to target elements in the CSS.&lt;/p&gt;

&lt;p&gt;for example: we can use &lt;code&gt;document.querySelector('#id')&lt;/code&gt; instead of &lt;code&gt;document.getElementByID('id')&lt;/code&gt;&lt;/p&gt;

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

&lt;p&gt;You must be thinking, why do people use &lt;code&gt;querySelector&lt;/code&gt; when we have &lt;code&gt;getElementByID&lt;/code&gt; and &lt;code&gt;getElementByClassName&lt;/code&gt; and the reason is the flexibility of selecting the elements by &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;classname&lt;/code&gt; and &lt;code&gt;element&lt;/code&gt; name itself and also because this syntax is used in jquery, a very popular library used in web development.&lt;/p&gt;

&lt;h3&gt;
  
  
  querySelectorAll() 🧑‍🤝‍🧑
&lt;/h3&gt;

&lt;p&gt;This method works similar to &lt;code&gt;querySelector()&lt;/code&gt; but the one single difference is that it returns a &lt;strong&gt;nodelist&lt;/strong&gt; of the elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;NodeList&lt;/em&gt;&lt;/strong&gt; is an array-like collection of nodes. These nodes can be of various types such as elements, text nodes, comments, etc., that are part of a document.&lt;/p&gt;

&lt;p&gt;We will have to select an element out of that nodelist like we do in the array.&lt;/p&gt;

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

&lt;p&gt;You can see in the above image that it returns a list of all the elements I targeted using the class &lt;code&gt;btn&lt;/code&gt; and using the method &lt;code&gt;querySelectorAll()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can access different nodes of this nodelist like an array, i.e, &lt;code&gt;document.querySelectorAll('.btn')[1]&lt;/code&gt; to access the second element.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  getAttribute() and setAttribute() 🔖
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;getAttribute()&lt;/code&gt; method is used to access the attributes of an element once its selected in the DOM, which means you will have to first select it using either &lt;code&gt;getElementByID&lt;/code&gt;, &lt;code&gt;className&lt;/code&gt; or &lt;code&gt;querySelector&lt;/code&gt; methods.&lt;/p&gt;

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

&lt;p&gt;I accessed the mod button's attribute id using the &lt;code&gt;getAttribute()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;This is useful when you want to access the attributes of an element for example a link which you want to access from the &lt;code&gt;anchor (&amp;lt;a&amp;gt;)&lt;/code&gt; tag or &lt;code&gt;image (&amp;lt;img&amp;gt;)&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;We can also set values of an attribute using the &lt;code&gt;setAttribute()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setAttribute()&lt;/code&gt; takes 2 inputs, first one is the attribute you want to select and the second one is the value of that attribute you want to set.&lt;/p&gt;

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

&lt;p&gt;In the above image you can see that I selected the &lt;code&gt;class&lt;/code&gt; attribute of the element by accessing it by it's id &lt;code&gt;mod&lt;/code&gt; and changed it to &lt;code&gt;hashnode&lt;/code&gt; and that's why the button lost it's properties of the &lt;code&gt;btn&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;In easy terms you can read it like this in steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Select the element with mod id using &lt;code&gt;document.querySelector('#mod')&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the attribute of this element to hashnode using &lt;code&gt;setAttribute('class', 'hashnode')&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Accessing the text inside the elements 💬
&lt;/h2&gt;

&lt;p&gt;As now we know how to access the elements, now its time to access the &lt;strong&gt;&lt;em&gt;text/data&lt;/em&gt;&lt;/strong&gt; inside the elements.&lt;/p&gt;

&lt;p&gt;This can be done using following properties of the document object.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;textContent&lt;/li&gt;
&lt;li&gt;innerHTML&lt;/li&gt;
&lt;li&gt;innerText&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  textContent 📝
&lt;/h3&gt;

&lt;p&gt;textContent property returns the text inside the element.&lt;/p&gt;

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

&lt;p&gt;In the above image I accessed the &lt;code&gt;btn&lt;/code&gt; element and then accessed the text inside that element.&lt;/p&gt;

&lt;h3&gt;
  
  
  innerText ✍️
&lt;/h3&gt;

&lt;p&gt;Also returns the text inside the element.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  innerHTML 🕸️
&lt;/h3&gt;

&lt;p&gt;returns the HTML content of the element, meaning that if there is any &lt;code&gt;html&lt;/code&gt; tag inside the element it will also be returned here.&lt;/p&gt;

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

&lt;p&gt;I had those ✨ emojis in the span tag so it returned the span tags as well.&lt;/p&gt;

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

&lt;p&gt;If you found this blog post helpful, please consider &lt;strong&gt;sharing&lt;/strong&gt; it with others who might benefit. You can also follow me for more upcoming content on &lt;strong&gt;JavaScript&lt;/strong&gt;, &lt;strong&gt;React&lt;/strong&gt;, and other &lt;strong&gt;Web Development&lt;/strong&gt; topics.&lt;/p&gt;

&lt;p&gt;For paid collaboration, you can mail me at: &lt;a href="//mail.shubhsharma19@gmail.com"&gt;gmail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="//twitter.com/shubhstwt"&gt;Twitter&lt;/a&gt;, &lt;a href="//linkedin.com/in/shubhsharma19"&gt;LinkedIn&lt;/a&gt;, and &lt;a href="//github.com/shubhxg"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for Reading :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Easiest Explanation of DOM, Even a 6 year old can understand</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Mon, 08 Apr 2024 07:24:52 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/easiest-explanation-of-dom-even-a-6-year-old-can-understand-3njl</link>
      <guid>https://dev.to/shubhsharma19/easiest-explanation-of-dom-even-a-6-year-old-can-understand-3njl</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;When learning web development I am sure many folks have seen or heard about this term called "DOM" and many people actually take it as a scary, hard to understand topic even before they start to learn about it.&lt;/p&gt;

&lt;p&gt;In this article I will try to explain DOM &lt;em&gt;(not that fast and furious one)&lt;/em&gt; in the easiest possible way.&lt;/p&gt;

&lt;p&gt;So let's get started.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffommjntitgaaei5u1vds.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffommjntitgaaei5u1vds.gif" alt="Dom in car" width="498" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is DOM?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Document object model&lt;/strong&gt; provides a way to represent the structure and content of an HTML document as a &lt;strong&gt;&lt;em&gt;"tree of objects"&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can think of it as a family tree from top to bottom but the only difference here is that it has only 1 top node called root node and is also known as the &lt;strong&gt;"document"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Document&lt;/strong&gt; is the top node and the other child nodes of this document are called Elements of the HTML page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffh0i3ns633h8fhf5rglp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffh0i3ns633h8fhf5rglp.jpg" alt="Family tree diagram" width="538" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each element within the HTML file (such as &lt;code&gt;&amp;lt;body&amp;gt;,&amp;lt;head&amp;gt;, &amp;lt;title&amp;gt;,&amp;lt;div&amp;gt;, &amp;lt;section&amp;gt;, &amp;lt;h1&amp;gt;,&amp;lt;p&amp;gt;&lt;/code&gt; etc.) becomes a separate node in the tree which are children of the root node (document).&lt;/p&gt;

&lt;p&gt;While the root node is commonly known as the &lt;strong&gt;"document node"&lt;/strong&gt;, it's important to note that other elements like &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; can also serve as parent nodes to their own child nodes, forming a hierarchical structure within the DOM tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  Document is an object
&lt;/h2&gt;

&lt;p&gt;Remember that &lt;strong&gt;"document"&lt;/strong&gt; is an object, you can access the data of that object by logging out the document, &lt;code&gt;console.log(document)&lt;/code&gt;, You can see lots of properties and methods of the document object.&lt;/p&gt;

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

&lt;p&gt;This is how we will be accessing different properties and methods of document object.&lt;/p&gt;

&lt;h2&gt;
  
  
  DOM Tree Structure
&lt;/h2&gt;

&lt;p&gt;This is how actual DOM Tree looks like:&lt;/p&gt;

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

&lt;p&gt;&lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; itself is a child of document, &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; are children of &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt; are children of &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; tag and so on.&lt;/p&gt;

&lt;p&gt;Now I know few of you might be wondering &lt;em&gt;"But you said top node is document!"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I know I know, and &lt;strong&gt;I can explain!&lt;/strong&gt; 🗣️&lt;/p&gt;

&lt;h2&gt;
  
  
  Window
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Window&lt;/strong&gt; is not the top node, its just the &lt;strong&gt;"area where document is displayed"&lt;/strong&gt;, basically your browser tab in which you are reading this article right now, open a new tab and there will be new window. Yep that's the window!&lt;/p&gt;

&lt;p&gt;So we can not call it the root node as we are only referring to the document and HTML related content for this tree, or can we? 🤔 It's a debatable topic.&lt;/p&gt;

&lt;p&gt;What are your opinions on this? Leave them in the comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  text node
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;"text node"&lt;/strong&gt; is a type of node that represents text content inside elements of the HTML page. Such as text inside the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/p&amp;gt;&lt;/code&gt; tags, or text inside &lt;code&gt;&amp;lt;title&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/title&amp;gt;&lt;/code&gt; tags, that text is represented as a text node in the DOM.&lt;/p&gt;

&lt;p&gt;It is a child of elements that contain textual content like paragraph tag or heading tag, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some more info about DOM
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Document object model is a programming interface that allows &lt;strong&gt;&lt;em&gt;"JavaScript"&lt;/em&gt;&lt;/strong&gt; to interact with web pages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It provides a way to represent the content of an HTML document as a tree of objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JavaScript interacts with the HTML using the DOM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can access and manipulate the HTML content on your webpage using the document object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Document object is a part of Browser environment only.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What can we do with DOM?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We can &lt;strong&gt;&lt;em&gt;"access"&lt;/em&gt;&lt;/strong&gt; the contents of the HTML document with JavaScript which gives us the power to manipulate content on the webpage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can &lt;strong&gt;&lt;em&gt;"traverse"&lt;/em&gt;&lt;/strong&gt; through different elements and select the ones which we need to manipulate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can &lt;strong&gt;&lt;em&gt;"edit"&lt;/em&gt;&lt;/strong&gt; and change the content on the webpage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can &lt;strong&gt;&lt;em&gt;"create"&lt;/em&gt;&lt;/strong&gt; our own content on the webpage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We can &lt;strong&gt;&lt;em&gt;"delete"&lt;/em&gt;&lt;/strong&gt; the content from the webpage easily.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;&lt;strong&gt;DOM&lt;/strong&gt; really gives so much power to manipulate the data dynamically on the webpage without ever writing a new HTML page from the scratch.&lt;/p&gt;

&lt;p&gt;If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more upcoming content on &lt;strong&gt;JavaScript, React, and other web Development topics&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For paid collaboration, you can mail me at: &lt;a href="mailto:mail.shubhsharma19@gmail.com"&gt;mail.shubhsharma19@gmail.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="//twitter.com/shubhstwt"&gt;Twitter&lt;/a&gt;, &lt;a href="//linkedin.com/in/shubhsharma19"&gt;LinkedIn&lt;/a&gt;, and &lt;a href="//github.com/shubhxg"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for Reading :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>dom</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>REST API Explanation for 6 years old</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Thu, 04 Apr 2024 09:57:00 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/rest-api-explanation-for-6-years-old-14md</link>
      <guid>https://dev.to/shubhsharma19/rest-api-explanation-for-6-years-old-14md</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Imagine you have a vending machine. 💭🤖&lt;/p&gt;

&lt;p&gt;When you approach the vending machine and press the buttons, it performs certain actions and gives you the desired item. &lt;/p&gt;

&lt;p&gt;Now, think of the vending machine as a &lt;code&gt;SERVER&lt;/code&gt; (a computer which serves or gives) and you as a &lt;code&gt;CLIENT&lt;/code&gt; (a computer which need something) wants to interact with it.&lt;/p&gt;

&lt;h2&gt;
  
  
  REST API
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;REST API&lt;/strong&gt; is like a set of rules that the vending machine and you follow to communicate with each other.&lt;/p&gt;

&lt;p&gt;It defines how you can ask the &lt;code&gt;SERVER&lt;/code&gt; for specific items and how the &lt;code&gt;SERVER&lt;/code&gt; responds to your requests. 🟢 &lt;/p&gt;

&lt;p&gt;In this case, the vending machine has different buttons &lt;code&gt;(ENDPOINTS)&lt;/code&gt; representing different items (RESOURCES) you can get. &lt;/p&gt;

&lt;p&gt;For example, button 1 for &lt;strong&gt;snacks&lt;/strong&gt;, button 2 for &lt;strong&gt;drinks&lt;/strong&gt;, and 3 for &lt;strong&gt;candies&lt;/strong&gt; 🍭.&lt;/p&gt;

&lt;p&gt;Each button has a unique label &lt;code&gt;(URL)&lt;/code&gt; that tells the vending machine what you want.&lt;/p&gt;

&lt;p&gt;To get the item you want, you press the &lt;strong&gt;button&lt;/strong&gt; (send an HTTP request) to the vending machine. &lt;/p&gt;

&lt;p&gt;The machine understands the request (GET, POST, PUT, DELETE, etc) and performs the necessary action according to your request,  It may give you the item you requested &lt;code&gt;(RESPONSE)&lt;/code&gt; or perform some other action based on your request, or sometimes might show you an error if item is not available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stateless Behavior
&lt;/h2&gt;

&lt;p&gt;The vending machine is &lt;strong&gt;"stateless"&lt;/strong&gt;, meaning it doesn't remember anything about your previous requests. &lt;/p&gt;

&lt;p&gt;Each time you press a button, you provide all the necessary information for the machine to understand what you want.&lt;/p&gt;

&lt;p&gt;This is because &lt;code&gt;HTTP&lt;/code&gt; is a &lt;strong&gt;stateless protocol&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;Similar to a vending machine, in a REST API, the server and client (which could be a website or mobile app) interacts using some rules. &lt;/p&gt;

&lt;p&gt;The client (You) send requests to the server's specific URLs (ENDPOINTS) to get or manipulate data. &lt;/p&gt;

&lt;p&gt;The server (Vending machine) understands the requests and responds accordingly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REST APIs&lt;/strong&gt; are widely used in web development to create services that allow different systems and applications to communicate and exchange data.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>api</category>
      <category>explanation</category>
    </item>
    <item>
      <title>Async/Await Easiest Explanation For Beginners</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Thu, 25 Jan 2024 11:24:26 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/asyncawait-easiest-explanation-for-beginners-44di</link>
      <guid>https://dev.to/shubhsharma19/asyncawait-easiest-explanation-for-beginners-44di</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Before learning about async/await you should know what and how promises work in JavaScript. You can read about promises &lt;a href="https://dev.to/shubhsharma19/promises-in-javascript-for-beginners-2h46"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is async/await?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;async/await&lt;/code&gt; were introduced in 2017, Before that managing asynchronous operations in javaScript was primarily done using promises.&lt;/p&gt;

&lt;p&gt;While promises helped in the "callback hell" issue, they still introduced a certain level of complexity, especially when dealing with multiple async operations in sequence.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Async/await&lt;/code&gt; was introduced to make working with asynchronous code even more easy and readable. It provides a syntax that looks and behaves like synchronous code.&lt;/p&gt;

&lt;p&gt;It is still built on top of Promises and is compatible with all existing Promise-based APIs.&lt;/p&gt;

&lt;p&gt;Creating an async function using async keyword 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;functionName&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;h2&gt;
  
  
  async function working
&lt;/h2&gt;

&lt;p&gt;An async function created with async keyword will always return a promise.&lt;/p&gt;

&lt;p&gt;There could be 2 situations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You return a promise explicitely like this
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// explicitely returning the promise&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;functionName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Some data&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You return any other value like number, object, string, array, etc, but here the JavaScript engine wraps that value inside a promise object to ensure that the value returned is a promise.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will find second case in more professional code some examples such as &lt;code&gt;fetch()&lt;/code&gt; or promisified version of &lt;code&gt;fs&lt;/code&gt; methods or other promisified methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;asyncFunction2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// this gets wrapped inside a promise object&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;String&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;h2&gt;
  
  
  What about the await keyword?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; keyword is used inside an &lt;code&gt;async&lt;/code&gt; function to pause the function execution until a promise settles.&lt;/p&gt;

&lt;p&gt;To understand this first understand how callstack and basic functions work 👇&lt;/p&gt;

&lt;p&gt;JavaScript thread works by pushing the variables and function calls inside &lt;code&gt;callstack&lt;/code&gt;. This function is not popped out of the stack until the code inside the function is completed executing.&lt;/p&gt;

&lt;p&gt;But when its an &lt;code&gt;async&lt;/code&gt; function with &lt;code&gt;await&lt;/code&gt; keyword used inside, the story is different.&lt;/p&gt;

&lt;p&gt;When JavaScript thread sees the &lt;code&gt;await&lt;/code&gt; keyword inside the &lt;code&gt;async function&lt;/code&gt;, the function is suspended from the &lt;code&gt;callstack&lt;/code&gt;, meaning it is popped out until the promise is completed (resolved or rejected) and once promise is finished the function is again pushed on the stack and it continues from where it left off with the result of the promise.&lt;/p&gt;

&lt;p&gt;This is where a lot of beginners get confused. So a point to note here is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Point: Await keyword will only suspend the parent async function in which it is written. This means that JS thread is not blocked and can continue the other work out of the function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Code example and Explanation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;returnData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Promise is resolved&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="mi"&gt;3000&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchDataWithAwait&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;promisificaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;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;data&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;Thread waited because of await keyword!&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="nf"&gt;fetchDataWithAwait&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;Thread is still free to work!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation of code:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;JS thread sees the &lt;code&gt;returnData&lt;/code&gt; function which returns a resolved value after 3 seconds (setTimeout function is making sure of 3 seconds delay)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JS thread sees the &lt;code&gt;fetchDataWithAwait&lt;/code&gt; function declaration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JS thread now sees the &lt;code&gt;fetchDataWithAwait()&lt;/code&gt; call and pushes the function on the callstack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JS thread finds the &lt;code&gt;await&lt;/code&gt; keyword and suspends this function from the callstack until promise is resolved (3 seconds)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JS thread continues to next line and prints &lt;code&gt;"Thread is still free to work!"&lt;/code&gt; on the console&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now since the JS thread is idle, it pushes the &lt;code&gt;async function&lt;/code&gt; back to the callstack and starts execution from where it left off.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;"Promise is resolved"&lt;/code&gt; is printed on the console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;"Thread waited because of await keyword!"&lt;/code&gt; is now printed.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx6553jo8fudvulqiivs8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx6553jo8fudvulqiivs8.png" alt="output for the code" width="452" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use aync/await when we already had promises?
&lt;/h2&gt;

&lt;p&gt;Because the syntax of promises is confusing while await makes it more clear and readable.&lt;/p&gt;

&lt;p&gt;Promises create a lot of confusion when you are dealing with API calls again and again in your code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;async/await&lt;/code&gt; fixes that confusion. &lt;/p&gt;

&lt;p&gt;Remember that &lt;code&gt;async/await&lt;/code&gt; still uses promises behind the scenes but it just makes the syntax more readable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using try/catch() to handle errors
&lt;/h2&gt;

&lt;p&gt;Since &lt;code&gt;async/await&lt;/code&gt; does not handle errors by default so we wrap them inside a try and catch() block of code.&lt;/p&gt;

&lt;p&gt;try() is used to try the piece of code we wrote and catch() catches any error thrown by the api calls like in this example. This ensures the proper error handling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchDataWithAwait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;promisificaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Promise&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;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;data&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;Thread waited because of await keyword!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;fetchDataWithAwait&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Once you know how to use &lt;code&gt;async/await&lt;/code&gt; Async js becomes really easy. Took me many days to properly grasp all this but I tried to put this in easiest way possible.&lt;/p&gt;

&lt;p&gt;After all it is said that if you can not explain something in easy terms then you don't know it properly. ;)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Promises in JavaScript for Beginners</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Sun, 21 Jan 2024 12:39:54 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/promises-in-javascript-for-beginners-2h46</link>
      <guid>https://dev.to/shubhsharma19/promises-in-javascript-for-beginners-2h46</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;When working with async code in javascript you sometimes need to wait for the code to return data before further processing of data. &lt;/p&gt;

&lt;p&gt;For example: reading a file takes some times or fetching data from the server can take time and since this is async task it gets delegated. &lt;/p&gt;

&lt;p&gt;However we need this data before we can process and do anything further in our code. This is where promises can be super helpful.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Promise?
&lt;/h2&gt;

&lt;p&gt;Before promises were introduced, managing asynchronous operations was done using callbacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callbacks&lt;/strong&gt; are functions that are passed as arguments to another function and are executed later, once the asynchronous operation is complete.&lt;/p&gt;

&lt;p&gt;However, as programs grew in complexity, managing callbacks became challenging, leading to what's often referred to as "callback hell" or "pyramid of doom."&lt;/p&gt;

&lt;p&gt;This is a situation where nested callbacks make the code hard to read and maintain.&lt;/p&gt;

&lt;p&gt;Promises were introduced to address these issues and provide a better/cleaner way to handle asynchronous operations.&lt;/p&gt;

&lt;p&gt;So what the heck is a &lt;strong&gt;Promise&lt;/strong&gt;? well, a promise is an object representing the eventual completion or failure of an async operation and its resulting value.&lt;/p&gt;

&lt;p&gt;In short if an async task is completed with success it returns a resolve object with the data and if the task fails it returns a reject object with an error. Promise ensures that once the operation is completed, you can do anything you wanted depending on the completion or failure of the operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  States of a promise
&lt;/h2&gt;

&lt;p&gt;A Promise has three states:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;pending: Promise is pending and its neither resolved nor resolved&lt;/li&gt;
&lt;li&gt;resolved: Once promise is resolved it returns data to &lt;code&gt;then()&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;rejected: Once promise is rejected the error is returned to &lt;code&gt;catch()&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A basic example of a promise used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Creating a promise &lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Simulating async operation using setTimeout&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&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;success&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Data fetched successfully&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;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error fetching data&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="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This piece of code means a new promise is created which does an async operation such as &lt;code&gt;setTimeout()&lt;/code&gt; and once its completed it either resolves the promise which means success or it gets reject with an error.&lt;/p&gt;

&lt;p&gt;Once created a promise you can consume that promise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Consuming the promise&lt;/span&gt;
&lt;span class="nx"&gt;myPromise&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="c1"&gt;//default one, gets executed no matter resolve or reject&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;finally&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the promise is resolved then it is returned to &lt;code&gt;.then()&lt;/code&gt; method of that promise, if promise is rejected then it gets returned to &lt;code&gt;.catch()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;In both the cases, you can do a task after the promise is completed.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to handle data returned by the promise?
&lt;/h2&gt;

&lt;p&gt;This can be done with the help of chaining of then() and catch()&lt;/p&gt;

&lt;p&gt;Once the data is returned from the &lt;code&gt;resolve&lt;/code&gt; you can get that data in &lt;code&gt;then()&lt;/code&gt; method and then return it into a new &lt;code&gt;then&lt;/code&gt; chain and then you can use that data and the error is caught by &lt;code&gt;catch()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;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;Async task completed!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="c1"&gt;//data returned with resolve&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;error&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;//handling data with chaining of then() and catch()&lt;/span&gt;
&lt;span class="nx"&gt;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="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;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&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="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="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&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="nx"&gt;err&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;



</description>
      <category>javascript</category>
      <category>promises</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Async programming in JavaScript for beginners</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Wed, 03 Jan 2024 09:19:01 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/async-programming-in-javascript-for-beginners-4c7b</link>
      <guid>https://dev.to/shubhsharma19/async-programming-in-javascript-for-beginners-4c7b</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Before understanding what is async programming and what is the need of async programming we need to understand the basic difference between sync and async and even before that we need to understand what does it mean when we say that javascript is single threaded.&lt;/p&gt;

&lt;h2&gt;
  
  
  Single threaded nature of JS
&lt;/h2&gt;

&lt;p&gt;Programming languages can be of 2 types based on their threaded nature.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Multi-threaded: (Java, C++, C#, Golang, etc.)&lt;/li&gt;
&lt;li&gt;Single-threaded: (JavaScript, etc.)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The single-threaded nature of JavaScript means that it processes one task at a time, and tasks are executed in a sequential manner.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the heck is main thread in JS?
&lt;/h2&gt;

&lt;p&gt;The main thread is like a worker doing one task at a time. It's the only worker in our example.&lt;/p&gt;

&lt;p&gt;This worker (main thread) reads your JavaScript code line by line and does each task one after another. It's diligent but can only focus on one thing at a time.&lt;/p&gt;

&lt;p&gt;In the world of web browsers, this worker not only runs your code but also takes care of updating what you see on the screen (HTML and CSS stuff). This means, when you click a button or load a page, the worker is responsible for making it happen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Difference between sync vs. async
&lt;/h2&gt;

&lt;p&gt;In programming terms, &lt;strong&gt;Synchronous&lt;/strong&gt; means &lt;strong&gt;together or sequential&lt;/strong&gt; but one thing at a time and &lt;strong&gt;Asynchronous&lt;/strong&gt; means &lt;strong&gt;parallel, context switching between tasks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This means sync operations are done one after another (main thread) without context switching between operations and if one operation is a time-consuming operations that could potentially block the main thread and lead to a slower code execution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhhjbr4et5o1rmat20f32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhhjbr4et5o1rmat20f32.png" alt="sync vs. async task diagram" width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Asynchronous programming?
&lt;/h2&gt;

&lt;p&gt;Async programming is basically managing tasks that might take some time to complete, such as &lt;strong&gt;I/O operations, file reading, network request, fetching data from a server, converting data into JSON, or waiting for a user to click a button&lt;/strong&gt;, without blocking the execution of the rest of the code.&lt;/p&gt;

&lt;p&gt;Some operations are not completed immediately and require some time to be completed, these operations are completed in the future, could be after 1 second, or 2 second, or 20 seconds, or even more, and since we are not sure when these tasks will be completed we make sure that these tasks are async in nature.&lt;/p&gt;

&lt;p&gt;Because these tasks if taking too long might block the execution of the main thread so they are delegated so that the main thread is able to continue its work.&lt;/p&gt;

&lt;p&gt;But who controls this behaviour of thread?&lt;/p&gt;

&lt;p&gt;The answer is "runtime environment". It handles context switching between tasks to ensure efficient execution of the code. It can be in nodejs, browser or bun whatever.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does javascript being single threaded does operations in parallel?
&lt;/h2&gt;

&lt;p&gt;With the help of async functions such as setTimeout(), fs.readFile(), fs.writeFile(), fetch() and so more.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example of a sync vs. async operation using built-in async functions 👇&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//flow of the code execution is denoted with numbers in bracket.&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// (1)&lt;/span&gt;

&lt;span class="k"&gt;for&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// (2)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// (3)&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (4)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;now let's see how async operations gets executed in js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//flow of the code execution is denoted with numbers in bracket.&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="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (1)&lt;/span&gt;
&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="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="s1"&gt;Async operation finished!&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="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// (4)&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="s1"&gt;world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (2)&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="s1"&gt;hehe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (3)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above example, I have used &lt;code&gt;setTimeout()&lt;/code&gt; function which is an async function, it does performs something after a certain period of time which is given in milliseconds (5000ms = 5 seconds).&lt;/p&gt;

&lt;p&gt;Let's see the full flow of the code execution 👇&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;hello&lt;/code&gt; is printed at first,&lt;/li&gt;
&lt;li&gt;then the setTimeout function is read by the thread and since it is an async operation it is delegated and thread continues to do the next &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;world!&lt;/code&gt; is printed on screen, after that it continues on next line&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;hehe&lt;/code&gt; is printed.&lt;/li&gt;
&lt;li&gt;and once all the sync tasks are completed, thread becomes idle and now it's free to take back the task which it delegated and now after 5 seconds, it prints &lt;code&gt;Async operation completed.&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How can we write our own async
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;By wrapping up the in-built js async functions inside our own created &lt;/li&gt;
&lt;li&gt;By using Promises.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here I will talk about the first approach only.&lt;/p&gt;

&lt;p&gt;for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 5 seconds&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printsHelloAfterDelay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;callbackFn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;printsHelloAfterDelay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;printHello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;printHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello User!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="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="s1"&gt;This is printed first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here, I created a sync function but inside there is an async operation happening.&lt;/p&gt;

&lt;p&gt;That operation is delegated and once finishe it is pushed onto the callback queue, untill the thread is free it just waits there, and once thread is idle, it is invoked.&lt;/p&gt;

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

&lt;p&gt;Async functions and async programming helps a lot in tasks which are needed to be completed in future and we dont want our thread to be blocked by the time taking operation.&lt;/p&gt;

&lt;p&gt;In next blog I will talk about the architecture that allows this async operations in js.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Git Foundations</title>
      <dc:creator>Shubh Sharma</dc:creator>
      <pubDate>Wed, 21 Jun 2023 13:26:38 +0000</pubDate>
      <link>https://dev.to/shubhsharma19/linux-file-structure-explained-24l7</link>
      <guid>https://dev.to/shubhsharma19/linux-file-structure-explained-24l7</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;In your tech journey, I am sure you must have come across the term Git.&lt;/p&gt;

&lt;p&gt;Git stands as one of the most powerful and most used tools ever created.&lt;/p&gt;

&lt;p&gt;In this article, we will deep dive into the world of Git, explore its history, and the reason it was created, will discuss the version control systems and more.&lt;/p&gt;

&lt;h2&gt;
  
  
  A little history of Git
&lt;/h2&gt;

&lt;p&gt;Before Git, the Linux kernel development team used a proprietary Version control system called BitKeeper. However, a dispute arose between the BitKeeper creators and the Linux Kernel team.&lt;/p&gt;

&lt;p&gt;As a consequence, the free-of-charge access to BitKeeper was revoked.&lt;/p&gt;

&lt;p&gt;This incident led to the creation of Git by Linus Torvalds and Linux Kernel Development Team. They learned from their experiences with BitKeeper and set out specific goals in mind for the new system.&lt;/p&gt;

&lt;p&gt;(Here is a quick meme 👇 you will get it If you know about Linus Torvalds)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuuwf6g34u5xb6id19b7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuuwf6g34u5xb6id19b7d.png" alt="Meme on Linux" width="503" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first public release of Git was in April 2005. It was made available for the public to download and quickly gained attention within the open-source community due to its performance and flexibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Version Control System and why do we need it?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F536y56ye7qczhhx7k06v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F536y56ye7qczhhx7k06v.png" alt="version control system diagram" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Version Control System is a very powerful tool that helps you track changes in your files over time. It is a software utility that manages changes to a filesystem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The concept of version control came some decades ago. In the early days of software development, programmers faced challenges when it came to keeping track of code changes and coordinating with team members. As projects grew larger and more complex, the need for a systematic approach to version control was needed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Early version control systems were primarily focused on file-level operations, where developers used to manually manage different versions of files by creating copies or using naming conventions such as v1.0 or v1.1 etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;However, these methods proved to be error-prone and not very efficient.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the 1980s, a Centralized version control system was introduced.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After some time a distributed version control (DVCS) idea came up and one of these was BitKeeper.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;VCS tracks the addition, deletion, and modification actions applied to files and directories.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Types of Version Control Systems
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8rjansx9j1vt6fkv2vdz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8rjansx9j1vt6fkv2vdz.png" alt="Types of Version control system diagrams with centralized and distributed" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Centralized VCS&lt;/strong&gt;: In a CVCS, there is a central repository that stores the entire history of the project. Developers check out files from this central repository, make changes, and then commit their changes back to the repository. Examples of CVCS include SVN and Perforce.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Distributed VCS:&lt;/strong&gt; In a DVCS, each developer maintains a local copy of the entire repository, including the complete history. Developers can commit changes to their local repository and share those changes with others by pushing them to a central repository or directly to other developers. Examples of DVCS include Git and Mercurial.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Benefits of using Git
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fast:&lt;/strong&gt; Git is designed to be fast and efficient.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Keeps track of changes:&lt;/strong&gt; Git maintains a complete history of all modifications made to the codebase, including the ability to view differences between different versions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easy to use:&lt;/strong&gt; Git is very user-friendly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Non-linear:&lt;/strong&gt; Git supports a non-linear workflow, allowing developers to create multiple branches to work on different features or bug fixes simultaneously. This enables parallel development, where team members can collaborate on separate branches without interfering with each other's work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Distributed:&lt;/strong&gt; Git is a distributed version control system (DVCS), which means that every developer working on a project has a complete copy of the repository, including the entire history of changes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fib5srryp2psm9h7y331r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fib5srryp2psm9h7y331r.png" alt="Distributed and Non-linear diagram" width="800" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Can handle large codebases:&lt;/strong&gt; Git can handle projects of any size, including large-scale applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  If Git is so powerful then why do we need GitHub?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;GitHub or similar platforms are used as a centralized place to distribute code from. While Git provides the core functionality for version control, these platforms allow more functionality making it easier to work on large-scale projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can have your repo stored on GitHub, you pull the code from the repo into your local machine, make some changes then make commits and then you can push those changes from your local machine to the remote repo on GitHub.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are more reasons people use GitHub, some of them include:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Backups&lt;/li&gt;
&lt;li&gt;Better collaborations&lt;/li&gt;
&lt;li&gt;Open Source Codebases&lt;/li&gt;
&lt;li&gt;Issue tracking&lt;/li&gt;
&lt;li&gt;PR tracking&lt;/li&gt;
&lt;li&gt;Project management&lt;/li&gt;
&lt;li&gt;Integration of more tools such as CI/CD tools, etc.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is a Commit?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8knu5i8kcdur00h909vo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8knu5i8kcdur00h909vo.png" alt="Git Commit diagram" width="686" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In Git, a commit is a snapshot of the changes made to a repository at a particular point in time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It represents a set of changes made to the codebase, such as the addition or modification or deletion of the files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each commit has a unique identifier, called a hash, that allows you to refer to it and track the changes made to the code over time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you create a commit, you will also need to add a commit message that describes the changes you've made.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can think of a commit as a photograph that you take of a file every time you commit a change.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphxtc74kahebkpymq4ax.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphxtc74kahebkpymq4ax.jpg" alt="meme on commit in git" width="476" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Branch?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Git branches are like those separate smaller branches on a tree.&lt;/li&gt;
&lt;li&gt;They're separate paths of development that come from the main branch.
-Each branch can have its own set of changes and updates.&lt;/li&gt;
&lt;li&gt;The changes that you made on a branch will not affect the main branch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Here is how commits and branches look like if you visualize 👇&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnetcrvwvp8kbdm9btbqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnetcrvwvp8kbdm9btbqw.png" alt="Visual representation of branches and commits" width="800" height="545"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And here is how it will look if you visualize them in a tree-like structure 👇&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31fyw4xvweedslcc7q4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31fyw4xvweedslcc7q4g.png" alt="Tree structure of a git graph" width="493" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Repo/Repository?
&lt;/h2&gt;

&lt;p&gt;In Git, a repository is used as a place where you store and manage your codebase or project. It contains all the files, directories, and the history related to your work.&lt;/p&gt;

&lt;p&gt;A repository is commonly called a repo in short. Repos can be divided into 2 types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Local Repository&lt;/strong&gt;: A local repository is a copy of the changes on your local machine (your computer), and it contains the complete history and files of your work. You can create a local repository by initializing Git in a directory using the command git init. It sets up the necessary Git files to track changes to your files within that directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Remote Repository&lt;/strong&gt;: A remote repository is hosted on a remote server, such as GitHub, GitLab, or Bitbucket. It acts as a centralized location where multiple developers can collaborate on a project. You can push changes from your local repository to the remote repository and pull changes made by others into your local repository. Remote repositories allow for efficient collaboration, version control, and backup of your project.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Difference between Local and Remote Repo
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1s8mz5cudem25om98kw1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1s8mz5cudem25om98kw1.png" alt="Local vs. remote branch difference" width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The local Repo is in the computer saved locally while the remote repo is saved in the server as a central storage backup option. It also allows better collaboration, GUI features, project management features, issue tracking, pull request tracking and more.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Fork? 🍴
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A Fork is a way to create a copy of a repository that belongs to someone else.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you fork a repository, you're essentially making a "duplicate" of that repository and creating your "own" version of it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Changes made in your forked repository won't affect the original project or anyone else's fork of it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h2&gt;
  
  
  How to create a fork?
&lt;/h2&gt;

&lt;p&gt;To create a fork, you will need to create a GitHub account first. Follow these steps to create a fork 👇&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Go to GitHub&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a new account and if you have one then log in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on the fork button of the repo of your choice (In this case I am forking the LinkFree repo)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftccglz9m9rqhmi9anphs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftccglz9m9rqhmi9anphs.png" alt="fork button on github in top right" width="800" height="89"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now it will ask you to give it a name, the default name is also fine.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwwhd2w561xeepnt6rsi8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwwhd2w561xeepnt6rsi8.png" alt="Putting details to make a fork" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on the Create Fork button and it will create a fork of that repo for you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5xrlwl788gt2ovjvmbm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5xrlwl788gt2ovjvmbm.png" alt="fork created of linkfree" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice how the forked repo says "forked from EddieHub/LinkFree" This is important to remind you that this is a fork, not the original repo.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once you have forked the repo in GitHub, you can use the git remote add "URL" command to make a local copy of this fork on your computer.&lt;/p&gt;

&lt;p&gt;We will discuss all these commands in the next article.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Pull Request?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A pull request is a report of all the changes made.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's say that someone else wants to make a change in your repo. They can do that by creating a fork of that repo and make changes in their forked repo so that it doesn't interfere with your work.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once they are satisfied with their changes, they can merge their work in your repo from Fork to original repo by asking you if you want to pull these changes into your original repo.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To do that, they can create a separate branch and commit their changes to that branch and then they can create a Pull Request to merge these changes into your original repo.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;These changes then can be merged once the maintainer of that repo allows.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

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

&lt;p&gt;With that said, I hope this article helped you in understanding the foundations of Git.&lt;/p&gt;

&lt;p&gt;In the next part, I will discuss more about the advanced concepts such as setting up the git and working with the git commands, making commits and more.&lt;/p&gt;

&lt;h2&gt;
  
  
  More Resources to checkout
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Kunal Kushwaha Git YouTube Video&lt;/li&gt;
&lt;li&gt;Git for Noobs series on YouTube&lt;/li&gt;
&lt;li&gt;Wikipedia&lt;/li&gt;
&lt;li&gt;bitbucket.org&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
