<?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: vidhi-thakur</title>
    <description>The latest articles on DEV Community by vidhi-thakur (@vidhithakur).</description>
    <link>https://dev.to/vidhithakur</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%2F657524%2F1f79e1f8-4930-43b5-9427-5b805acd1313.jpeg</url>
      <title>DEV Community: vidhi-thakur</title>
      <link>https://dev.to/vidhithakur</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vidhithakur"/>
    <language>en</language>
    <item>
      <title>Frontend Concepts</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Wed, 03 Dec 2025 14:15:38 +0000</pubDate>
      <link>https://dev.to/vidhithakur/frontend-concepts-3fne</link>
      <guid>https://dev.to/vidhithakur/frontend-concepts-3fne</guid>
      <description>&lt;p&gt;Moving towards strong frontend specialization. Here I will add short blogs on the topics that are needed for a senior frontend developer. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>What Happens When You Add Arrays or Objects in JS? 🤯</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Sun, 06 Jul 2025 11:41:14 +0000</pubDate>
      <link>https://dev.to/vidhithakur/what-happens-when-you-add-arrays-or-objects-in-js-15g6</link>
      <guid>https://dev.to/vidhithakur/what-happens-when-you-add-arrays-or-objects-in-js-15g6</guid>
      <description>&lt;p&gt;When we generally talk about the + operator, the tricky problem we think about is: Why does &lt;code&gt;1 + 2 + "3"&lt;/code&gt; result in &lt;code&gt;"33"&lt;/code&gt; whereas &lt;code&gt;"1" + 2 + 3 = "123"&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;But do we ever stop and think — can we add arrays with numbers? 🤯&lt;/p&gt;

&lt;p&gt;Sounds silly, but what really happens when JavaScript encounters such an expression?&lt;/p&gt;

&lt;p&gt;It all depends on how JavaScript parses code. You see, when you write &lt;code&gt;[] + 1&lt;/code&gt;, you don’t get an error. Instead, you get the result &lt;code&gt;"1"&lt;/code&gt;. Let’s understand how and why this happens.&lt;/p&gt;

&lt;p&gt;When JavaScript encounters a non-primitive value in an expression, it tries to convert it to a primitive using its internal ToPrimitive algorithm. First, it calls &lt;code&gt;.valueOf()&lt;/code&gt;, and if that doesn't return a primitive, it uses &lt;code&gt;.toString()&lt;/code&gt;, which for an empty array, gives &lt;code&gt;""&lt;/code&gt;. So effectively &lt;code&gt;[] + 1&lt;/code&gt; becomes &lt;code&gt;"" + 1&lt;/code&gt;, which gives the string &lt;code&gt;"1"&lt;/code&gt;due to string concatenation.&lt;/p&gt;

&lt;p&gt;But that’s not all. Get this, &lt;code&gt;({}).toString()&lt;/code&gt; returns &lt;code&gt;"[object Object]"&lt;/code&gt;, so when you add &lt;code&gt;[] + {}&lt;/code&gt;, the result is &lt;code&gt;"[object Object]"&lt;/code&gt;. BUT 😏 when you do &lt;code&gt;{} + []&lt;/code&gt;, you get…&lt;/p&gt;

&lt;p&gt;… a big fat &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Why? Again, it’s all about how JS parses code. In this case, JavaScript sees &lt;code&gt;{}&lt;/code&gt; as an empty code block, not an object. So it interprets &lt;code&gt;+[]&lt;/code&gt; as a unary plus operation, which coerces the empty array into &lt;code&gt;0&lt;/code&gt;. Want the original result? Wrap it in parentheses &lt;code&gt;({} + []) = "[object Object]"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I’ll be sharing a follow-up post soon with more mind-bending JavaScript edge cases.&lt;/p&gt;

&lt;p&gt;How did you find this one? Helpful, confusing, or just plain wild? Drop your thoughts in the comments 👇&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Read this blog before you start your data structure and algorithm journey!!!</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Wed, 02 Apr 2025 14:24:19 +0000</pubDate>
      <link>https://dev.to/vidhithakur/read-this-blog-before-you-start-your-data-structure-and-algorithm-journey-47a6</link>
      <guid>https://dev.to/vidhithakur/read-this-blog-before-you-start-your-data-structure-and-algorithm-journey-47a6</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/vidhithakur/dsas-intro-to-your-development-journey-254d" class="crayons-story__hidden-navigation-link"&gt;Intrduction to DSA&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/vidhithakur" class="crayons-avatar  crayons-avatar--l  "&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%2Fuser%2Fprofile_image%2F657524%2F1f79e1f8-4930-43b5-9427-5b805acd1313.jpeg" alt="vidhithakur profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/vidhithakur" class="crayons-story__secondary fw-medium m:hidden"&gt;
              vidhi-thakur
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                vidhi-thakur
                
              
              &lt;div id="story-author-preview-content-2242700" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/vidhithakur" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&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%2Fuser%2Fprofile_image%2F657524%2F1f79e1f8-4930-43b5-9427-5b805acd1313.jpeg" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;vidhi-thakur&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/vidhithakur/dsas-intro-to-your-development-journey-254d" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Jan 29 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/vidhithakur/dsas-intro-to-your-development-journey-254d" id="article-link-2242700"&gt;
          Intrduction to DSA
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/javascript"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;javascript&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/datastructures"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;datastructures&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/webdev"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;webdev&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
            &lt;a href="https://dev.to/vidhithakur/dsas-intro-to-your-development-journey-254d#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              &lt;span class="hidden s:inline"&gt;Add Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            4 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>javascript</category>
      <category>datastructures</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Graph is fun, check this blog to find out how</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Wed, 29 Jan 2025 10:28:45 +0000</pubDate>
      <link>https://dev.to/vidhithakur/graph-is-fun-check-this-blog-to-find-out-how-335a</link>
      <guid>https://dev.to/vidhithakur/graph-is-fun-check-this-blog-to-find-out-how-335a</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/vidhithakur" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&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%2Fuser%2Fprofile_image%2F657524%2F1f79e1f8-4930-43b5-9427-5b805acd1313.jpeg" alt="vidhithakur"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/vidhithakur/get-a-gist-of-graph-here-4295" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Get a gist of graph data structure here...&lt;/h2&gt;
      &lt;h3&gt;vidhi-thakur ・ Dec 10 '24&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#datastructures&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#graph&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>datastructures</category>
      <category>javascript</category>
      <category>graph</category>
      <category>programming</category>
    </item>
    <item>
      <title>Intrduction to DSA</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Wed, 29 Jan 2025 10:24:29 +0000</pubDate>
      <link>https://dev.to/vidhithakur/dsas-intro-to-your-development-journey-254d</link>
      <guid>https://dev.to/vidhithakur/dsas-intro-to-your-development-journey-254d</guid>
      <description>&lt;p&gt;In this blog we will discuss the most basic unit of information that we need to understand to start data structures and algorithms. &lt;/p&gt;

&lt;p&gt;Most frontend developers like myself often question the need to do DSA when not much of DSA is required in our development usually. But  sometimes it does makes a difference. Every point for performance improvement is critical if you don't want users to close the tab because your website is too slow. Understanding of time complexity and efficient algorithms helps you write better (more optimized) code which helps with scalability.  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;In this blog we will understand: -&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are data structures? &lt;/li&gt;
&lt;li&gt;What is time complexity?&lt;/li&gt;
&lt;li&gt;What is space complexity? and &lt;/li&gt;
&lt;li&gt;How do we simplify complexity analysis?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's start...&lt;/p&gt;

&lt;h2&gt;
  
  
  What are data structures?
&lt;/h2&gt;

&lt;p&gt;Data structures can be defined by 3 parameters: -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it is a collection of data,&lt;/li&gt;
&lt;li&gt;the data items has a relationship or a linking factor, and&lt;/li&gt;
&lt;li&gt;we can perform functions or operations on the data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For an example we can analyze an array arr = [12, 54, 86, 3, 94] based on our parameters. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;collection of data (✔️)&lt;/li&gt;
&lt;li&gt;a relationship or a linking factor &lt;em&gt;&lt;u&gt;{each array element has a unique index}&lt;/u&gt;&lt;/em&gt; (✔️)&lt;/li&gt;
&lt;li&gt;can perform functions or operations on the data &lt;em&gt;&lt;u&gt;{ex: arr.push(7)}&lt;/u&gt;&lt;/em&gt;(✔️)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Therefore, an array is a valid data structure. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a time complexity?
&lt;/h2&gt;

&lt;p&gt;Time complexity is the relationship between input and operations performed in an algorithm. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;input ↑ operations ↑&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Time complexity is not calculated in seconds because of two factors: -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;on different computers, the time taken for an algorithm can differ because of the difference in the hardware,&lt;/li&gt;
&lt;li&gt;on the same computer, it is also not certain that the time taken by an algorithm (with same input) is same all the time. (not convinced for this point myself - if someone could point out the logic in the comments that would be great).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Therefore, because of the above-mentioned points, the time complexity is not calculated by time taken in seconds but rather by a factor that remains across all the hardware. That common factor is &lt;em&gt;&lt;u&gt;number of operations&lt;/u&gt;&lt;/em&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Regardless of the hardware, the number of operations would be same for an algorithm if the provided input is the same. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's understand time complexity with an example too. &lt;/p&gt;

&lt;p&gt;Example: We need to get the sum of N numbers. Now, we can solve this task with 2 approaches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Approach 1: we solve this with the formula (N * (N-1))/2 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Approach 2: we get the sum by, sum = (N-1) + (N-2) + ... + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;&lt;em&gt;For approach 1: -&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
This approach has 3 operations. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If N = 3: number of operations = 3&lt;/li&gt;
&lt;li&gt;If N = 300: number of operations = 3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means, if value of N increases, the number of operations remains the same. Time complexity of this approach is constant. &lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;em&gt;For approach 2: -&lt;/em&gt;&lt;/u&gt;&lt;br&gt;
This approach has (N-1) + (N-2) operations. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(N-1) = operations to get (N-1) numbers before N, and &lt;/li&gt;
&lt;li&gt;(N-2) because addition operation happens (N-2) times after getting all the numbers&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;So, this approach requires 2N - 3 operations total. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;The number of operations will increase as the value of N increases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To represent time complexity, we use Big O notation. For approach 1, since the time complexity remains constant regardless of the input value, the time complexity is O(1) which is called as order of 1. And for approach 2, the number of operation increases linearly as the input value increases, so the time complexity is O(2N-3).&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%2Fzmdse99s50tnu1t413iv.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%2Fzmdse99s50tnu1t413iv.png" alt="O(1) vs O(N) graph" width="800" height="692"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Some common time complexities: -
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;O(1): constant,&lt;/li&gt;
&lt;li&gt;O(N): linear - algorithm has to traverse array of inputs or write a loop once,&lt;/li&gt;
&lt;li&gt;O(logN): binary search algorithm,&lt;/li&gt;
&lt;li&gt;O(NlogN): merge sort algorithm,&lt;/li&gt;
&lt;li&gt;O(N^2), O(N^3), etc: case of nested loops. 2 loops, complexity O(N^2) and so on...&lt;/li&gt;
&lt;li&gt;O(2^N): fibonacci,&lt;/li&gt;
&lt;li&gt;O(N!)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h3&gt;
  
  
  Simplify complexity analysis (Big O): -
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Drop the constants. Ex: N^2 + &lt;del&gt;3&lt;/del&gt;
&lt;/li&gt;
&lt;li&gt;Drop insignificant terms. Ex: N^3 + &lt;del&gt;N&lt;/del&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The reason for above mentioned suggestion is that when N has a big value say N=10000, 3 operations or N operations would not add much significance to N^2 or N^3 operations. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Do not drop another input M. Ex: O(N^3 + M + 2) can be simplified as O(N^3 + M) 
&amp;gt; The reason for above mentioned suggestion is because inputs can be provided such that M has a significant value say 10,000 whereas N = 1. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Based on above explanation, the time complexity for approach 2 can be simplified to O(N). &lt;/p&gt;

&lt;h2&gt;
  
  
  What is space complexity?
&lt;/h2&gt;

&lt;p&gt;Space complexity is the relationship between input and auxiliary space required by an algorithm. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For space complexity, we do not consider the space that is required by the input and only consider extra space required by the algorithm.&lt;/li&gt;
&lt;li&gt;Similar to time complexity, Big O notation is used to represent space complexity. &lt;/li&gt;
&lt;li&gt;If the auxiliary space used by algorithm remains constant as the input size changes, the space complexity is O(1), and so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Pointers to think after this blog:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Why O(1) is better than O(N)?&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>datastructures</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Get a gist of graph data structure here...</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Tue, 10 Dec 2024 21:03:27 +0000</pubDate>
      <link>https://dev.to/vidhithakur/get-a-gist-of-graph-here-4295</link>
      <guid>https://dev.to/vidhithakur/get-a-gist-of-graph-here-4295</guid>
      <description>&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%2Fs6plm018jkgk2zvbqh2y.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6plm018jkgk2zvbqh2y.gif" alt="hello" width="272" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This blog is for developers who want to get the gist of a concept and not beat around the bush. We will understand graph data structure in this post. Let's start. &lt;/p&gt;

&lt;h2&gt;
  
  
  What are Graphs?
&lt;/h2&gt;

&lt;p&gt;Graphs are non-linear data structures with 2 components namely edges and nodes. &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%2Flh4.googleusercontent.com%2Fproxy%2FxnlIenZmt7FfPRLaz5VaB0Joqix-Mg-xu0yFIt1wDb7utTGyC7aTomqT8MxBzJXgrj1URXuruwKva89JGaBDj_fcZZmDdUTTfcEfW2fLeiUBYCzCRt7dTiU" 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%2Flh4.googleusercontent.com%2Fproxy%2FxnlIenZmt7FfPRLaz5VaB0Joqix-Mg-xu0yFIt1wDb7utTGyC7aTomqT8MxBzJXgrj1URXuruwKva89JGaBDj_fcZZmDdUTTfcEfW2fLeiUBYCzCRt7dTiU" alt="graph visual representation" width="512" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A node is&lt;/em&gt;: -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;a basic unit of graph,&lt;/li&gt;
&lt;li&gt;also known as vertex or vertices, and&lt;/li&gt;
&lt;li&gt;can be &lt;a href="https://docs.google.com/document/d/1nzaYOuR55ZTf0q3v5AzVQbrVQpmmWF6d8giMnwFxMM0/edit?usp=sharing" rel="noopener noreferrer"&gt;labeled or unlabeled&lt;/a&gt;. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;An edge is&lt;/em&gt;:-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;a connection between two nodes,&lt;/li&gt;
&lt;li&gt;also known as arcs, and&lt;/li&gt;
&lt;li&gt;can be labeled or unlabeled. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Types of graph: -
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;u&gt;Null graph&lt;/u&gt;: It has no edges,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Trival graph&lt;/u&gt;: It has only one node,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Cyclic graph&lt;/u&gt;: It contains at least one cycle,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Cycle graph&lt;/u&gt;: All nodes are connected to make one cycle,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Directed graph&lt;/u&gt;: Its edges have direction,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Undirected graph&lt;/u&gt;: Its edges do not have any direction,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Weighted graph&lt;/u&gt;: Some value is assigned to each edge, &lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Connected graph&lt;/u&gt;: We can visit any other node from one node (it does not have to be a unique edge),&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Unconnected graph&lt;/u&gt;: At least one node is disconnected from a node,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Regular graph&lt;/u&gt;: Every vertex has the same number of neighbours,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Complete graph&lt;/u&gt;: Every node is connected to another node by a unique edge,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Directed acyclic graph&lt;/u&gt;: A directed graph with no cycles,&lt;/li&gt;
&lt;li&gt;
&lt;u&gt;Bipartite graph&lt;/u&gt;: Its nodes can be divided into 2 sets such that no edges exist among the sets. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How do we store graphs?
&lt;/h2&gt;

&lt;p&gt;There are 2 ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Adjacent matrix, and &lt;/li&gt;
&lt;li&gt;Adjacent list&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;em&gt;&lt;u&gt;adjacent matrix&lt;/u&gt;&lt;/em&gt; is a 2-D matrix representing graph data structure. The rows and columns represent nodes and the value in the matrix represents the edges. &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%2Fm0ydr4xjarosg4j7mvxj.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%2Fm0ydr4xjarosg4j7mvxj.png" alt="adjacent matrix" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;small&gt;In the above image, 4 nodes are connected by the edges. Node A is connected to all the nodes and thus the value is added as 1 in the cells where A (either row or column) intersects with rows or columns of other nodes. Node B is only connected to Node A resulting in a value 1 in the cell where node B intersects with node A and other cells have a value 0. &lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The time complexity to add or delete an edge for the adjacent matrix is O(1). It can be used when: -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the graph has the maximum possible edges,&lt;/li&gt;
&lt;li&gt;there is no memory constraint, and&lt;/li&gt;
&lt;li&gt;the graph has a complex structure. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;em&gt;&lt;u&gt;adjacent list&lt;/u&gt;&lt;/em&gt; is storing the graph with the help of multiple linked lists or arrays. The rows represent the node (check the image below), and the values present in the rows represent neighbour. &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%2F3aolweivs5e9orv4a3qy.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%2F3aolweivs5e9orv4a3qy.png" alt="adjacent list" width="670" height="262"&gt;&lt;/a&gt;&lt;br&gt;
&lt;small&gt;In the above image, there are 5 nodes. Node A is connected to node B and node D which is why the first array has those values. Similarly, node B is connected to node A, node C, node D, and node E and so the second array has those nodes in the second array. &lt;/small&gt;&lt;/p&gt;

&lt;p&gt;The time complexity for retrieving or deleting an edge is O(n) and the time complexity for adding an edge is O(1). The adjacent list can be used when: -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the  node has fewer edges,&lt;/li&gt;
&lt;li&gt;there are memory constraints, and&lt;/li&gt;
&lt;li&gt;the graph is less complex.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Fig: Visual comparison between adjacent list and adjacent matrix.
&lt;/h4&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%2Fwat-images.s3.ap-south-1.amazonaws.com%2Fimages%2Fps%2Fadjacency-list-to-adjacency-matrix.svg" 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%2Fwat-images.s3.ap-south-1.amazonaws.com%2Fimages%2Fps%2Fadjacency-list-to-adjacency-matrix.svg" alt="comparson" width="2387" height="915"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Use cases for the graphs
&lt;/h3&gt;

&lt;p&gt;One popular example of graph data structure is: in a field of football each player can be considered a node and their interactions represent edges. &lt;/p&gt;

&lt;p&gt;Graphs are used in similar scenarios as previous example, such as: -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;in social networks where everyone is a node,&lt;/li&gt;
&lt;li&gt;in computer networks where PCs and routers are the nodes,&lt;/li&gt;
&lt;li&gt;in the transportation network, and so on...&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>datastructures</category>
      <category>javascript</category>
      <category>graph</category>
      <category>programming</category>
    </item>
    <item>
      <title>Getting Real-Time Updates in React JS: Polling vs WebSocket</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Tue, 18 Jul 2023 04:41:49 +0000</pubDate>
      <link>https://dev.to/vidhithakur/getting-real-time-updates-polling-vs-websocket-io4</link>
      <guid>https://dev.to/vidhithakur/getting-real-time-updates-polling-vs-websocket-io4</guid>
      <description>&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%2Fs6plm018jkgk2zvbqh2y.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6plm018jkgk2zvbqh2y.gif" alt="hello" width="272" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Usually, when a client gets the data from a server, it sends a request to the server, and the server then sends the response (or requested data) back. The issue with this process is we don't get the new data on the client side as soon as the new data is available to the server. &lt;/p&gt;

&lt;p&gt;Now let's say we want real-time updates on our web application. How do we do that!?&lt;/p&gt;

&lt;p&gt;First, let us understand what polling is. &lt;/p&gt;

&lt;h3&gt;
  
  
  Polling
&lt;/h3&gt;

&lt;p&gt;Well, polling is a technique in which the client sends the request repeatedly to the server after a fixed interval. This way, the client can check if the server has new information and update the client side accordingly.&lt;/p&gt;

&lt;p&gt;This can be implemented in React JS with the help &lt;code&gt;useEffect&lt;/code&gt; and &lt;code&gt;setInterval&lt;/code&gt;. Let's see how: -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    const interval = setInterval(() =&amp;gt; {
        // API call to get data from the server
    }, 5000)

    return () =&amp;gt; {
        clearInterval(interval);
    }
}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, a callback function inside &lt;code&gt;useEffect&lt;/code&gt; runs when the interface loads. The callback function uses &lt;code&gt;setInterval&lt;/code&gt; to make an API call to the server and gets the data. The API call happens after every 5 seconds.&lt;/p&gt;

&lt;h4&gt;
  
  
  Disadvantages of polling
&lt;/h4&gt;

&lt;p&gt;Even though polling can get us the updates from the server every few intervals, it has disadvantages. Issues with polling are: -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It increases the network load because the client has to keep making the API calls after fixed intervals.&lt;/li&gt;
&lt;li&gt;Low latency because the server processes the API request every time the request is sent.&lt;/li&gt;
&lt;li&gt;Delayed update since a new connection establishes after a fixed interval which can potentially delay an update.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To overcome these issues, we can use something like WebSocket API and get the updates on the client side as soon as the new data is available on the server (real-time).&lt;/p&gt;

&lt;h3&gt;
  
  
  WebSocket API
&lt;/h3&gt;

&lt;p&gt;The WebSocket API uses something known as WebSocket communication. The WebSocket communication allows the client to have a real-time two-way connection and provides events and javascript methods that interact with the server. &lt;/p&gt;

&lt;p&gt;Implementation in React JS: -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
    // Establish the connection
    const socket = new WebSocket(api_url);

    // To receive messages from the server
    socket.onmessage = (event) =&amp;gt; {
        const msg = event.data;
        // Do something with msg
    }

    return () =&amp;gt; {
        // Close the connection
        socket.close();
    }

}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example is a basic implementation of WebSocket API. Step 1 is to establish the connection. When &lt;code&gt;new WebSocket&lt;/code&gt; is invoked, it initiates a connection between the client and the specified URL.&lt;/p&gt;

&lt;p&gt;After the connection is established, the client can get the messages via an onmessage event. The client can utilize other events and methods such as onerror, onopen, onclose, send(), close(), etc.&lt;/p&gt;

&lt;p&gt;Follow the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications" rel="noopener noreferrer"&gt;link&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;In the case of WebSocket API, the connection remains open, unlike polling, where the connection terminates after the client receives the data from the server. &lt;/p&gt;

&lt;p&gt;Since the WebSocket connection remains open, the client receives real-time data updates from the server without delay. Additionally, the client does not have to make multiple API calls to fetch the updates reducing the load on the network.&lt;/p&gt;

&lt;p&gt;These factors make WebSocket connections favorable for apps requiring real-time updates like chat applications, collaborative editing tools, and many more.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The server sent events by Chat GPT API can be handled via WebSocket too. &lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Temporal Dead Zone (TDZ)</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Mon, 09 May 2022 18:20:32 +0000</pubDate>
      <link>https://dev.to/vidhithakur/temporal-dead-zone-tdz-1kp</link>
      <guid>https://dev.to/vidhithakur/temporal-dead-zone-tdz-1kp</guid>
      <description>&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%2Fs6plm018jkgk2zvbqh2y.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6plm018jkgk2zvbqh2y.gif" alt="hello" width="272" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's understand TDZ and why does it exist. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Temporal Dead Zone (TDZ)&lt;/strong&gt; is an area of a block where a variable cannot be accessed until it is initialized. Let's see a simple example to understand better.&lt;/p&gt;

&lt;p&gt;Example 1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Welcome ",name)

let name = "Vidhi"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code written above, the area above the variable &lt;code&gt;name&lt;/code&gt; is initialized is &lt;em&gt;temporal dead zone&lt;/em&gt;. Notice a console log there. Well if we try to run this code, it will throw a reference error. &lt;br&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%2Fxabvmqubtozozktaf17n.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%2Fxabvmqubtozozktaf17n.png" alt="TDZ error example" width="743" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This happened because in the console.log statement we tried to access a value which was not yet initialized. Let's see one more example.&lt;/p&gt;

&lt;p&gt;Example 2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Welcome ",name)

var name = "Vidhi"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example has a small change when compared to example 1. Here the variable is declared with var. Now what it does is, when we run this code, there will be no error but the value of &lt;code&gt;name&lt;/code&gt; will be undefined. &lt;/p&gt;

&lt;p&gt;This happens because when a &lt;code&gt;var&lt;/code&gt; variable is hoisted, it is automatically initialized as undefined. This does not happen with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; and therefore the error occurs. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Introduction to React Context API</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Thu, 05 Aug 2021 19:29:18 +0000</pubDate>
      <link>https://dev.to/vidhithakur/react-context-api-4fk4</link>
      <guid>https://dev.to/vidhithakur/react-context-api-4fk4</guid>
      <description>&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%2Fs6plm018jkgk2zvbqh2y.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6plm018jkgk2zvbqh2y.gif" alt="hello" width="272" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is React Context API?
&lt;/h3&gt;

&lt;p&gt;React Context API is a React API that allows one to share state across an entire app. Instead of installing a state management library in your project or prop drilling, the React Context API provides a way for a React app to effectively produce global variables that can be passed around.&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%2Fp4bahzuqdu0pmny7eb4f.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%2Fp4bahzuqdu0pmny7eb4f.png" alt="Diagram" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  createContext hook: -
&lt;/h4&gt;

&lt;p&gt;React hook that is needed for this purpose is &lt;em&gt;createContext()&lt;/em&gt;. It returns a provider and a consumer. The provider, as the name suggests, provides a state to its children whereas the consumer uses the state. &lt;/p&gt;

&lt;p&gt;Example 1: -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { createContext } from "react";

// creating a context
const StateContext = createContext();

export default function App() {
  return (
    &amp;lt;StateContext.Provider value={56}&amp;gt;
      &amp;lt;ShowValue /&amp;gt;
    &amp;lt;/StateContext.Provider&amp;gt;
  );
}

function ShowValue() {
  return (
    &amp;lt;StateContext.Consumer&amp;gt;
      {(value) =&amp;gt; &amp;lt;p&amp;gt;The display value is: {value}.&amp;lt;/p&amp;gt;}
    &amp;lt;/StateContext.Consumer&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;To see the working implementation of the above example, click &lt;a href="https://codesandbox.io/s/createcontext-s8p97" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the example above, we created a context. Now think of creating a context as creating a data layer. After the context is created, we wrapped a component (i.e., &lt;em&gt;ShowValue component&lt;/em&gt;) in the data layer so that the data or value is accessible in that component. Finally, the data (or value) is fetched using the consumer. Note that the consumer obtains the value passed by the &lt;em&gt;provider&lt;/em&gt; without prop drilling. &lt;/p&gt;

&lt;p&gt;Using react Context API lets you avoid passing of props from parent component to children components. It's all good until you need to pass multiple values. In case of retrieving data from multiple parent contexts, a lot of nesting is introduced.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;CurrentUser.Consumer&amp;gt;
      {user =&amp;gt;
        &amp;lt;Notifications.Consumer&amp;gt;
          {notifications =&amp;gt;
            &amp;lt;header&amp;gt;
              Welcome back, {user.name}!
              You have {notifications.length} notifications.
            &amp;lt;/header&amp;gt;
          }
        &amp;lt;/Notifications.Consumer&amp;gt;
      }
    &amp;lt;/CurrentUser.Consumer&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Therefore, to make the code less verbose we use &lt;em&gt;useContext() hook&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  useContext hook: -
&lt;/h4&gt;

&lt;p&gt;The useContext() hook provides a way to pass the data or value to any component without using the consumer. Further, it makes the code more readable and reduces cognitive overhead (i.e., the used amount of working memory resources). &lt;/p&gt;

&lt;p&gt;With useContext() hook, the code in example 1 will look like this: -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { createContext, useContext } from "react";

const StateContext = createContext();
const useStateValue = () =&amp;gt; useContext(StateContext);

export default function App() {
  return (
    &amp;lt;StateContext.Provider value={56}&amp;gt;
      &amp;lt;ShowValue /&amp;gt;
    &amp;lt;/StateContext.Provider&amp;gt;
  );
}

function ShowValue() {
const value = useStateValue();
  return (
      &amp;lt;p&amp;gt;The display value is: {value}.&amp;lt;/p&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;To see the working implementation of the above example, click &lt;a href="https://codesandbox.io/s/usecontext-hook-fzh63?file=/src/App.js" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Note: To further manage the state of the app, &lt;em&gt;useReducer() hook&lt;/em&gt; is used.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;By using React Context API, we can produce global variables that can be passed around an entire application and avoid prop drilling. For this purpose, the createContext and the useContext hooks are used. In addition, the useReducer hook can also be used in the case of managing the state of the app.&lt;/p&gt;

&lt;p&gt;P.S. Context API should only be used when you need to share state between many components with a lot of nesting.&lt;/p&gt;

</description>
      <category>react</category>
      <category>api</category>
    </item>
    <item>
      <title>Accessing Object Properties in JavaScript</title>
      <dc:creator>vidhi-thakur</dc:creator>
      <pubDate>Wed, 30 Jun 2021 15:43:53 +0000</pubDate>
      <link>https://dev.to/vidhithakur/accessing-object-properties-in-javascript-2fja</link>
      <guid>https://dev.to/vidhithakur/accessing-object-properties-in-javascript-2fja</guid>
      <description>&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%2Ff9z6cchgmtnvwif3kv1x.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff9z6cchgmtnvwif3kv1x.gif" alt="objects--greeting" width="272" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Almost all JavaScript values have properties and accessing the said properties can be done in 3 ways, i.e.,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;dot notation&lt;/li&gt;
&lt;li&gt;square brackets &lt;/li&gt;
&lt;li&gt;object destructuring &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Dot notation
&lt;/h2&gt;

&lt;p&gt;To access a property of an object through dot notation, we must know the property name beforehand. &lt;/p&gt;

&lt;p&gt;Syntax: &lt;code&gt;expression.identifier&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's see an example to understand this better.&lt;/p&gt;

&lt;p&gt;Example 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const items = {
      food: "soup",
      "phone-1": "iphone 12"
}

// to access food property
items.food
// output =&amp;gt; soup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, &lt;code&gt;items&lt;/code&gt; is an object from which we need to access a property, and &lt;code&gt;food&lt;/code&gt; is an identifier. Accessing food property of items is easy enough. However, if we try to access the &lt;code&gt;phone-1&lt;/code&gt; property through dot notation, it won't work. Wondering why? Because it is not a valid identifier. So, how can we access that property? We'll cover that in the next section.&lt;/p&gt;

&lt;p&gt;Now you know how accessing a property from dot notation works. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The identifier should be a &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Identifier" rel="noopener noreferrer"&gt;valid identifier&lt;/a&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Square brackets
&lt;/h2&gt;

&lt;p&gt;To access a property of an object via square brackets, we do not need to know the property name beforehand, that is, the property name could be dynamic (determined at run time).&lt;/p&gt;

&lt;p&gt;Syntax: &lt;code&gt;expression[expression]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's understand this method through example 1. In example 1, there was an object &lt;code&gt;items&lt;/code&gt; and it had two properties. So, to access the &lt;code&gt;food&lt;/code&gt; property, we simply have to do this: &lt;code&gt;items[food]&lt;/code&gt;. Now about that second property, do you think we would be able to access that? &lt;/p&gt;

&lt;p&gt;If your answer is yes then you are correct.&lt;/p&gt;

&lt;p&gt;We can access the &lt;code&gt;phone-1&lt;/code&gt; property by doing this: &lt;code&gt;items["phone-1"]&lt;/code&gt;. When using square brackets, the expression between the brackets is &lt;em&gt;evaluated&lt;/em&gt; and converted to a string to get the property name. Whereas using dot notations only fetches the value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object destructuring
&lt;/h2&gt;

&lt;p&gt;Destructuring is a JavaScript expression that allows us to extract data from arrays, objects, and maps and set them into new, distinct variables. Object destructuring assigns the properties of an object to variables with the same names by default.&lt;/p&gt;

&lt;p&gt;See the example below: -&lt;/p&gt;

&lt;p&gt;Example 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const items = {
    food: "soup"
    "phone-1": "iphone 12"
}

const { food } = items;

food;
// output =&amp;gt; soup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The destructuring defines a variable &lt;code&gt;food&lt;/code&gt; with the value of property &lt;code&gt;food&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: The variable should have the same name as the property.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Further, to create a variable name different than the property name and to access the &lt;code&gt;phone-1&lt;/code&gt; property, we can use aliasing.&lt;/p&gt;

&lt;p&gt;Syntax: &lt;code&gt;const { identifier: aliasIdentifier } = expression;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For instance in example 2, to create a different variable name of &lt;code&gt;phone-1&lt;/code&gt; property, do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const {"phone-1": newVariable} = items

newVariable;
// output =&amp;gt; iphone 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;To access a property of an object via dot notation, the property name should be a &lt;em&gt;valid identifier&lt;/em&gt; and &lt;em&gt;known ahead of time&lt;/em&gt;. Whereas accessing the property of an object via square brackets is favorable when the property name is &lt;em&gt;dynamic&lt;/em&gt; or is &lt;em&gt;not a valid identifier&lt;/em&gt;. In addition, the object destructuring extracts the property directly into a variable. &lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Fun fact from Eloquent
&lt;/h3&gt;

&lt;p&gt;The elements in an array are stored as the array’s properties, using numbers as property names. Because you can’t use the dot notation with numbers and usually want to use a binding that holds the index anyway, you have to use the bracket notation to get at them.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Did you like this blog? Let me know in the comments below✌️. &lt;/p&gt;

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