<?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: Akansh Saxena</title>
    <description>The latest articles on DEV Community by Akansh Saxena (@akanshsaxena).</description>
    <link>https://dev.to/akanshsaxena</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%2F666250%2F47e069f5-7d5d-445c-a9c7-3bd501e4135f.JPG</url>
      <title>DEV Community: Akansh Saxena</title>
      <link>https://dev.to/akanshsaxena</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akanshsaxena"/>
    <language>en</language>
    <item>
      <title>this with arrow functions in JS</title>
      <dc:creator>Akansh Saxena</dc:creator>
      <pubDate>Mon, 12 Sep 2022 18:49:32 +0000</pubDate>
      <link>https://dev.to/akanshsaxena/this-with-arrow-functions-in-js-4df0</link>
      <guid>https://dev.to/akanshsaxena/this-with-arrow-functions-in-js-4df0</guid>
      <description>&lt;p&gt;Hello reader, &lt;em&gt;this&lt;/em&gt; has always been one of the most confusing topics in JavaScript. If you have some previous knowledge of programming languages like Java, then understanding &lt;em&gt;this&lt;/em&gt; would again be a much more difficult task because your mind will have to fight between your existing knowledge of &lt;em&gt;this&lt;/em&gt; in other languages and learning new concepts in JavaScript even though they don't overlap at all.&lt;/p&gt;

&lt;p&gt;Along with various other features and enhancements, ES6 brought arrow functions with it, and with the arrow functions introduction, the concept of &lt;em&gt;this&lt;/em&gt; started having a few more tweaks. To understand its concept and behaviour with arrow functions, some previous understanding of &lt;em&gt;this&lt;/em&gt; is definitely required. So, feel free to pause here and get some understanding of &lt;em&gt;this&lt;/em&gt; and then jump back to the topic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of contents:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What are arrow functions?&lt;/li&gt;
&lt;li&gt;
Understanding &lt;em&gt;this&lt;/em&gt; behaviour with arrow functions

&lt;ul&gt;
&lt;li&gt;In normal declaration&lt;/li&gt;
&lt;li&gt;Defined inside a global object&lt;/li&gt;
&lt;li&gt;In nested function call inside an object&lt;/li&gt;
&lt;li&gt;Global functions bind to an object&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What are arrow functions?&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Along with various other features, ES6 brought arrow functions with it too. They are newer, easier, and much cleaner ways of writing a function. They reduce the lines of code required to declare a function and come with small differences from the normal function declaration.&lt;br&gt;
Let's compare the normal and arrow function declaration syntax first.&lt;br&gt;
&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;//normal function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo1&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//arrow functions&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo2&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//this can further be written in much simpler way&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;As we see, arrow functions have much simpler syntax but the major difference is that arrow functions don't create their own binding but take up the binding of their parent object i.e. they inherit it from the parent scope (remember, the parent should be a non-function). To make it simpler, the arrow functions follow the same &lt;em&gt;this&lt;/em&gt; behaviour as it is in their parent. &lt;br&gt;
For normal functions, it totally depends on how and where they are called but for arrow functions, it depends on where they have been defined. In the above example, if we console &lt;em&gt;this&lt;/em&gt; inside both the functions then both &lt;em&gt;this&lt;/em&gt; will have a global object in them but the reason is quite different. Let's explore the same in next section.&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding &lt;em&gt;this&lt;/em&gt; behaviour with arrow functions&lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;this&lt;/em&gt; behaviour in arrow function can vary in different scenarios so let's break it into smaller parts and look through them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In normal declaration&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//normal function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo1&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//with arrow functions&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo2&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;foo1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;foo2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We saw in the above scenario, both consoles will print a global object(window object in case of browsers). Function foo1() is declared in a global context and is called in a global context, which is a normal function call. This means that the object calling foo1() is a global/window object(foo1() can also be called as a window.foo1()) and therefore, &lt;em&gt;this&lt;/em&gt; in having a value of the global object and hence, for a normal function call, &lt;em&gt;this&lt;/em&gt; always points to object calling that function. Whereas, foo2 has been declared in a global context whose &lt;em&gt;this&lt;/em&gt; always points to a global object and hence, in arrow function &lt;em&gt;this&lt;/em&gt; takes up the value of its parent binding, here global context and points to global/window object.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Defined inside a global object&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//normal function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;foo1&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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="c1"&gt;//with arrow functions&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;foo2&lt;/span&gt; &lt;span class="p"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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;obj1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To call the normal function(foo1) inside obj1, we need to first access obj1 and then call foo1 through it. Hence, foo1 is not a normal function call and it has to be called through obj1. obj1 is an object and has created its own binding(&lt;em&gt;this&lt;/em&gt;) and calling foo1 through obj1 makes &lt;em&gt;this&lt;/em&gt; inside obj1 to have all properties of obj1 only and not global/window object. Whereas, if we look at obj2 and foo2, then even though foo2 is declared as a key inside obj2 but still it will have a value of &lt;em&gt;this&lt;/em&gt; inherited from its lexical scope i.e. obj2 because as defined earlier, arrow functions inherit &lt;em&gt;this&lt;/em&gt; property from their parent and here obj2, which is parent object to foo2 has been declared in global/window context which has &lt;em&gt;this&lt;/em&gt; binding to global/window context and therefore, passes same this (global/window) to foo2.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In nested function call inside an object&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//normal function call inside a method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;foo1&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo2&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="nx"&gt;foo2&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;obj1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;//arrow function call inside a method&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;foo3&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo4&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="nx"&gt;foo4&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;obj2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo3&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you look closer at the code, then you can guess the output for both consoles as it has already been explained in the above two points. If you have taken a guess and it comes to be the same as what we will discuss, then congratulations to you. &lt;/p&gt;

&lt;p&gt;So, in foo2 the console will print the global/window object and the reason is in the definition above, any normal function call will always point its &lt;em&gt;this&lt;/em&gt; to the global/window object. Here, foo1 is a method to obj1 but foo2 is a normal function declared inside this method and hence &lt;em&gt;this&lt;/em&gt; pointing to global/window object. In the second scenario, foo4 is an arrow function that takes up the &lt;em&gt;this&lt;/em&gt; from its lexical scope so here, foo4 is lexically scoped to foo3, and foo3's &lt;em&gt;this&lt;/em&gt; is pointing to obj2 object, and hence foo4 will also point to obj2.&lt;/p&gt;

&lt;p&gt;Here comes an interesting question. What if, foo3 is assigned with an arrow function instead of a normal function?&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;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;foo3&lt;/span&gt; &lt;span class="p"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo4&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="nx"&gt;foo4&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;obj2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo3&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, it is certain that foo4 will always take up its &lt;em&gt;this&lt;/em&gt; value from the parent non-function which in this case is obj2, and since, obj2 is a global object whose binding is to a global object and hence foo4's &lt;em&gt;this&lt;/em&gt; will also point to global/window object. So, we can see a drastic change in the behaviour of functions depending if they are normal or arrow functions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Global functions bind to an object&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//normal function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo1&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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;obj1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Akansh&lt;/span&gt;&lt;span class="dl"&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;foo2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;foo2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;//arrow function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo3&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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;obj2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Akansh&lt;/span&gt;&lt;span class="dl"&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;foo4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;foo4&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The given examples are no different from what we have seen earlier in the above examples. The only difference is that a global function is created which is now bound to another object and hence, the functions will now be treated as they were declared inside the object.&lt;/p&gt;

&lt;p&gt;foo2 carries foo1 bind to obj1 and hence, calling foo2() is just like calling obj1.foo1()(if foo1 was declared inside obj1) and therefore, foo2 will have its &lt;em&gt;this&lt;/em&gt; pointing to obj1 only. Similarly, foo4 carries foo3 bind to obj2 but here foo4 &lt;em&gt;this&lt;/em&gt; points to global/window object because we now know that arrow function inside the object will have &lt;em&gt;this&lt;/em&gt; pointing to object's binding and here its global/window object.&lt;/p&gt;

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

&lt;p&gt;For a normal function call, &lt;em&gt;this&lt;/em&gt; will always point to global/window object. A normal function defined inside an object will always takes up &lt;em&gt;this&lt;/em&gt; from the object written on adjacent left in the dot notation. Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;foo&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;//here, obj is the object calling foo and is on the adjacent left while writing in dot notation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For arrow function, the simplest explanation will be, &lt;em&gt;this&lt;/em&gt; will always inherit binding from its non-functional parent. If defined in global context, then &lt;em&gt;this&lt;/em&gt; in arrow function points to global/window object only.&lt;/p&gt;

&lt;p&gt;I believe that the explanation was of some help to you and provided some value. If there is something I might have missed or you would like me to address or if there are some suggestions, feel free to comment and reach out. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>jsinterview</category>
    </item>
    <item>
      <title>What actually is Virtual DOM?</title>
      <dc:creator>Akansh Saxena</dc:creator>
      <pubDate>Mon, 19 Jul 2021 14:40:57 +0000</pubDate>
      <link>https://dev.to/akanshsaxena/what-actually-is-virtual-dom-1b12</link>
      <guid>https://dev.to/akanshsaxena/what-actually-is-virtual-dom-1b12</guid>
      <description>&lt;p&gt;Hello Reader, recently I encountered a but obvious question from an interviewer, &lt;strong&gt;"What exactly is Virtual DOM?"&lt;/strong&gt;. For the next five minutes, I was explaining how it works and why is it efficient but this didn't answer the question asked. If you are already working with ReactJS or are new to it then you surely would have encountered the same question a gazillion times but unfortunately, not all of us have a clear understanding of what a virtual dom is.&lt;/p&gt;

&lt;p&gt;I then read a couple of articles, ReactJS documentation and watched a few videos, and came up with an understanding of Virtual DOM. Hence, I will try to give a proper explanation of what is a Virtual DOM and how does it help in the faster rendering of components.&lt;/p&gt;

&lt;h1&gt;
  
  
  Table of contents
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;What is DOM?&lt;/li&gt;
&lt;li&gt;What is Virtual DOM?&lt;/li&gt;
&lt;li&gt;How do rendering components work with Virtual DOM?&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  What is DOM?&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;If you are coming to learn about Virtual DOM, you definitely would be knowing DOM, but to give a small gist we will go through it. &lt;em&gt;It is perfectly fine to skip this part.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Document Object Model(DOM)&lt;/strong&gt; is a structured model representation of HTML and XML documents. In simple words, the elements present on a webpage can be viewed in form of a tree, where the nodes are parent HTML tags and the branches include their respective children tags.&lt;/p&gt;

&lt;p&gt;Consider this example of a form visible on a webpage:&lt;/p&gt;

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

&lt;p&gt;The DOM of this form will look like this:&lt;/p&gt;

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

&lt;p&gt;Yes, the DOM is actually similar to the HTML one must have written for the form. Then how is DOM different from any HTML document? HTML is what the server sends in response to a request whereas DOM is built by the browser on top of the HTML received. The key difference still being that DOM is a model of a document that has various APIs through which we can manipulate the HTML over time.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Virtual DOM?&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Now, since we have an understanding of DOM, so let's dive deep into the world of Virtual DOM.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here we will try to explore Virtual DOM using React components because what's better than finding an answer in the question itself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We will now create a simple React functional Component and further instead of rendering it, we will just try to log it in the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;DummyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;title&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="p"&gt;(&lt;/span&gt;
           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&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;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h3&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;dummy&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h3&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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="nc"&gt;DummyComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Calling the dummy component&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;Now check what has been logged in the console. Kaa-Boom 💥, an object has been logged that has all the details of the component you just created. Have a look at the object and its keys, the props key contains the children's elements along with the other props passed to the component. This is what a Virtual DOM looks like.&lt;/p&gt;

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

&lt;p&gt;By definition, &lt;strong&gt;a virtual DOM object is a representation of a DOM object, like a lightweight copy.&lt;/strong&gt; So, now we can understand this definition more clearly. Virtual DOM is an object representation of the actual DOM rendered, it has all the DOM properties but is only present in the memory and cannot impact what gets rendered on the webpage. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Manipulating Virtual DOM is faster as compared to manipulating the actual DOM, the reason being that the Virtual DOM is only present in memory and can easily be created or deleted.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;To get more clarity on the object logged, you can refer to this &lt;a href="https://youtu.be/i793Qm6kv3U?t=250" rel="noopener noreferrer"&gt;video&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  How do rendering components work with Virtual DOM?&lt;a&gt;&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;Now that we have a basic understanding of DOM and Virtual DOM, we will have a look at the differences between the process of rendering elements using DOM and Virtual DOM.&lt;/p&gt;

&lt;p&gt;In simple DOM manipulation, let's say we want to handle the click event for any button &lt;code&gt;&amp;lt;button id="btn"&amp;gt;Click me!&amp;lt;/button&amp;gt;&lt;/code&gt;, then we first need to have access to this element using the &lt;code&gt;getElementById('btn')&lt;/code&gt;, this method will traverse through the complete DOM and will find the element with the specific ID and then will perform the DOM manipulation using other traversing cycle. This process is simple if we have to deal with a small number of elements but let's say we have an application to view stock market trends, prices of various stocks, and related news, now in this application, we have thousands of components, the data inside them may change a couple of times within a second, and so handling this using simple DOM manipulation technique might be a tedious and time taking task. To overcome this drawback, React uses Virtual DOM for its DOM manipulation process.&lt;/p&gt;

&lt;p&gt;In React, every component keeps an eye on its state and other parameters on which rendering of that component depends. Let's say that in our component there is a state change and re-rendering of the component is required, here the Virtual DOM comes into the picture, the whole process involved in rendering is as follows: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;At the initial step, React will take a snapshot of the actual DOM currently present (DOM present before the re-render happens) and will store it in the memory, for now, we can call it Real Virtual DOM.&lt;/li&gt;
&lt;li&gt;Now, React will create a new Virtual DOM from scratch which has all the old components plus the new change, this means React doesn't know the previous DOM. It will consider all its components as new. The new Virtual DOM created is kept separate from the old Virtual DOM and doesn't do any change to the actual DOM.&lt;/li&gt;
&lt;li&gt;In the third step, React will compare both the Virtual DOMs and will find out the new change that has to be implemented and its position. This comparison is done using the &lt;strong&gt;"Diffing Algorithm"&lt;/strong&gt; and the process is called &lt;em&gt;"diffing"&lt;/em&gt;. There is a whole lot that does behind the scenes while performing diffing, you can learn more about it &lt;a href="https://reactjs.org/docs/reconciliation.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Once the change has been detected, React will now remove the old component from the actual DOM and paint this new component onto the actual DOM. This process goes like, firstly the component that has to be removed receives &lt;code&gt;componentWillUnmount()&lt;/code&gt; lifecycle method which removes it, after this the new component that has to be pained receives &lt;code&gt;componentWillMount()&lt;/code&gt; and &lt;code&gt;componentDidMount()&lt;/code&gt; that brings the new component to the frontend. &lt;strong&gt;This process of syncing Virtual DOM to the real DOM is acknowledged as Reconciliation.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This completes the workflow of Virtual DOM. This might seem a time taking process because it involves few crucial steps, but remember &lt;em&gt;most of these steps are being executed in Virtual DOM which is basically objects stored in the memory until the last step where we truly need to change the actual DOM.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;So, this sums up the Virtual DOM and how it helps React in rendering components. If there is something that I missed or you would like to add then please provide your valuable feedback. Also, if there is anything you would like me to write about in the future then please let me know.&lt;/p&gt;

&lt;p&gt;P.S. Trying my hands at writing for the first time.&lt;/p&gt;

</description>
      <category>react</category>
      <category>virtualdom</category>
      <category>virtualdominreact</category>
      <category>dom</category>
    </item>
  </channel>
</rss>
