<?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: Matt Zuckermann</title>
    <description>The latest articles on DEV Community by Matt Zuckermann (@mattzuckermann).</description>
    <link>https://dev.to/mattzuckermann</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%2F550329%2Fcc574ae8-2cda-477c-bd4b-5cd58b324ece.jpeg</url>
      <title>DEV Community: Matt Zuckermann</title>
      <link>https://dev.to/mattzuckermann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mattzuckermann"/>
    <language>en</language>
    <item>
      <title>Learning how to use JavaScript 'this' keyword</title>
      <dc:creator>Matt Zuckermann</dc:creator>
      <pubDate>Fri, 29 Jan 2021 01:37:38 +0000</pubDate>
      <link>https://dev.to/mattzuckermann/learning-how-to-use-javascript-this-keyword-490c</link>
      <guid>https://dev.to/mattzuckermann/learning-how-to-use-javascript-this-keyword-490c</guid>
      <description>&lt;h3&gt;
  
  
  Why I decided to blog about "this":
&lt;/h3&gt;

&lt;p&gt;I'm a big fan of the &lt;a href="https://syntax.fm"&gt;Syntax.fm&lt;/a&gt; podcast by &lt;a href="https://wesbos.com"&gt;Wes Bos&lt;/a&gt; and &lt;a href="https://leveluptutorials.com"&gt;Scott Tolinski&lt;/a&gt;, and I remember hearing an episode where Wes said something to the point of a developer really needing to build a site around the JavaScript &lt;code&gt;this&lt;/code&gt; keyword due to the mysterious output we developers so often get when attempting to use it in various contexts.&lt;/p&gt;

&lt;p&gt;Of course, there is a plethora of documentation on the topic already, and I don't intend to outshine any of that (I do love the MDN Web Docs). Rather, this blog series along with my site &lt;a href="https://usethis.dev"&gt;usethis.dev&lt;/a&gt; will hopefully be able to accomplish centralizing articles and documentation, as well as provide opportunities for developers to quiz themselves on content discussed.&lt;/p&gt;

&lt;p&gt;So without any further introduction, let's get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  What we'll be learning in this series:
&lt;/h3&gt;

&lt;p&gt;We'll be learning the basics of the JavaScript &lt;code&gt;this&lt;/code&gt; keyword in various contexts, the use of bind, the window object, block and function scope, and the difference between regular functions and arrow functions (and, in turn, the effects they have on the &lt;code&gt;this&lt;/code&gt; keyword).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AALuZB12--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z2uopzq1ebz381exor58.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AALuZB12--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z2uopzq1ebz381exor58.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What we'll be learning in this post:
&lt;/h3&gt;

&lt;p&gt;Today, we will be discussing the global &lt;code&gt;window&lt;/code&gt; object, block and function scope, JavaScript data types, and gaining a general understanding of what &lt;code&gt;this&lt;/code&gt; will output in a given environment.&lt;/p&gt;




&lt;h1&gt;
  
  
  This in Action
&lt;/h1&gt;

&lt;p&gt;As a brief overview, here is a succinct statement on the topic of &lt;code&gt;this&lt;/code&gt; from the MDN Web Docs:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In most cases, the value of this is determined by how a function is called (runtime binding).&lt;/p&gt;

&lt;p&gt;-- &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this"&gt;MDN&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While it may seem deceivingly simple, the &lt;code&gt;this&lt;/code&gt; keyword is not like other keywords in JavaScript. It is completely determined on where it is called. For example, in the code below, we have used &lt;code&gt;this&lt;/code&gt; not in a function, but simply within a bare HTML script tag.&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&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="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="c1"&gt;// Window { window: Window, self: Window, document, name: "..."} etc.&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;p&gt;You can also get the same result by opening up the developer tools in any browser, go to the console tab, and enter &lt;code&gt;this&lt;/code&gt; as shown below in Chrome:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PeNI2MV2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/npxp8755cgjxr3kfkqtx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PeNI2MV2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/npxp8755cgjxr3kfkqtx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reasoning behind the following output is that there is no other &lt;u&gt;&lt;em&gt;object&lt;/em&gt;&lt;/u&gt; (remember this key term for later) encapsulating the &lt;code&gt;this&lt;/code&gt; keyword from which it will assume that environment. Only the window object is encapsulating the &lt;code&gt;this&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;To further explain this, let's look at a couple other examples:&lt;/p&gt;

&lt;h4&gt;
  
  
  Examples
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example 1&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrapperFunction&lt;/span&gt; &lt;span class="o"&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="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;// Example 2&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrapperObjectWithFunction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;key&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do you think these will output? Take a moment before scrolling down to view the answer.&lt;/p&gt;

&lt;p&gt;What&lt;/p&gt;

&lt;p&gt;Are&lt;/p&gt;

&lt;p&gt;The&lt;/p&gt;

&lt;p&gt;Answers&lt;/p&gt;

&lt;p&gt;Going&lt;/p&gt;

&lt;p&gt;To&lt;/p&gt;

&lt;p&gt;Be&lt;/p&gt;

&lt;p&gt;Here?&lt;/p&gt;

&lt;h4&gt;
  
  
  Results
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example 1&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrapperFunction&lt;/span&gt; &lt;span class="o"&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="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="c1"&gt;// Window { window: Window, self: Window, document, name: "..."} etc.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example 2&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;wrapperObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;log&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="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="c1"&gt;// {log: ƒ}&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;You might be surprised to see that in our first example, what is logged out is once again the &lt;code&gt;window&lt;/code&gt; object. Now, why is that?&lt;/p&gt;

&lt;p&gt;We must first understand that JavaScript variables can contain expressions. A simple yet pointless expression could be&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A common misconception in metaphor that many programmers have is thinking of variables as a box "holding" a value (i.e. sum holding the value of 2).&lt;/p&gt;

&lt;p&gt;Instead, variables act rather as &lt;u&gt;pointers&lt;/u&gt;. Variables point to either &lt;em&gt;primitives&lt;/em&gt; (i.e. string, number, boolean, bigint, symbol, undefined, or null), &lt;em&gt;functions&lt;/em&gt;, or &lt;em&gt;objects&lt;/em&gt;. (If you'd like to go deeper in this topic rather than get a 10,000 foot view, you should definitely check &lt;a href="https://twitter.com/dan_abramov"&gt;Dan Abramov's&lt;/a&gt; course &lt;a href="https://justjavascript.com"&gt;Just JavaScript&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Thus, when we are logging &lt;code&gt;this&lt;/code&gt; within a function we are not in fact logging out the variables assigned the function. That would be a bit of an oxymoron since the variable is the evaluation of an expression. Put simply, a function is really just a group of expressions to perform a particular task. Thus, it would make sense that we get the &lt;code&gt;window&lt;/code&gt; object in the first example since the variable is defined within the global scope.&lt;/p&gt;

&lt;p&gt;Remember what we said about variables being pointers to either &lt;em&gt;primitives&lt;/em&gt;, &lt;em&gt;functions&lt;/em&gt;, or &lt;em&gt;objects&lt;/em&gt;? Well, the same attribute stands for object keys. In the second example, the &lt;code&gt;log&lt;/code&gt; key is pointing to the function with the &lt;code&gt;console.log&lt;/code&gt; method. The only difference is objects create their own block scope which changes the value of &lt;code&gt;this&lt;/code&gt;. In this instance, the function is being pointed to by the log key which is defined within the object pointed to by the objectWrapper variable. As a result, the function is not bound to the global window object but instead it is tied locally to the object it is defined, which is why we are logging out an object containing the key &lt;code&gt;{log: f}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In our next article, we will be looking further at the difference the &lt;code&gt;this&lt;/code&gt; keyword has within arrow functions as well as the implications block scope has with different variables.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
