<?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: Todd Chaffee</title>
    <description>The latest articles on DEV Community by Todd Chaffee (@tchaffee).</description>
    <link>https://dev.to/tchaffee</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%2F176018%2F52481077-3a5d-4219-bf4f-1b1e63c26b95.png</url>
      <title>DEV Community: Todd Chaffee</title>
      <link>https://dev.to/tchaffee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tchaffee"/>
    <language>en</language>
    <item>
      <title>How do you personally learn?</title>
      <dc:creator>Todd Chaffee</dc:creator>
      <pubDate>Sun, 14 Jul 2019 15:12:28 +0000</pubDate>
      <link>https://dev.to/tchaffee/how-do-you-personally-learn-203c</link>
      <guid>https://dev.to/tchaffee/how-do-you-personally-learn-203c</guid>
      <description>&lt;p&gt;Most of the programmers I have met over the years are really good at learning. You have to be somewhat good at learning to keep up with constantly and rapidly changing technology. And yet we don't talk much about the details of our own personal techniques for learning. Let's share in the comments our own strategies for quick learning. The more detailed you can get about what you do and why will be helpful. Don't make any assumptions that what you do is already common knowledge to other people.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript and Scope VI - Closures</title>
      <dc:creator>Todd Chaffee</dc:creator>
      <pubDate>Sat, 22 Jun 2019 15:38:54 +0000</pubDate>
      <link>https://dev.to/tchaffee/javascript-and-scope-vi-closures-2jhn</link>
      <guid>https://dev.to/tchaffee/javascript-and-scope-vi-closures-2jhn</guid>
      <description>&lt;p&gt;In &lt;a href="https://blog.toddbiz.com/blog/2019-06-03-javascript-and-scope-ii/"&gt;JavaScript and Scope II&lt;/a&gt; we saw that function scope is not determined by where a function is run. Instead, it is determined by where a function is declared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value of i when bar is declared inside foo: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&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;// logs 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/x3f1Ln8e/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But the example above doesn't fully illustrate lexical scope because the &lt;code&gt;bar&lt;/code&gt; function is also &lt;em&gt;run&lt;/em&gt; inside the &lt;code&gt;foo&lt;/code&gt; function. Let's run the &lt;code&gt;bar&lt;/code&gt; function outside of the &lt;code&gt;foo&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value of i when bar is declared inside foo: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;barFunc&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;barFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Runs in global scope but logs 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/so324k6x/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code above, we no longer run &lt;code&gt;bar&lt;/code&gt; inside of &lt;code&gt;foo&lt;/code&gt;. Instead we return the inner function without running it. Then we run the function on the last line of code: it runs in the global scope. Most importantly it runs &lt;em&gt;after&lt;/em&gt; &lt;code&gt;foo&lt;/code&gt; is done running. But it does not use the global &lt;code&gt;i&lt;/code&gt; variable. Instead it uses the &lt;code&gt;i&lt;/code&gt; variable from the function that has already finished running!&lt;/p&gt;

&lt;p&gt;The official name for what you just saw is closure. That's how simple closure is. All closure means is that even when a function is run outside of the scope it was declared in, it still uses the scope from where it was declared. In this case, inner function &lt;code&gt;bar&lt;/code&gt; uses the scope of outer function &lt;code&gt;foo&lt;/code&gt;.  This is true even though &lt;code&gt;foo&lt;/code&gt; has stopped running by the time we run &lt;code&gt;bar&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This breaks our expectation that the scope of &lt;code&gt;foo&lt;/code&gt; no longer exists when it stops running.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript and Scope V - let and const</title>
      <dc:creator>Todd Chaffee</dc:creator>
      <pubDate>Fri, 21 Jun 2019 16:23:49 +0000</pubDate>
      <link>https://dev.to/tchaffee/javascript-and-scope-v-let-and-const-ede</link>
      <guid>https://dev.to/tchaffee/javascript-and-scope-v-let-and-const-ede</guid>
      <description>&lt;p&gt;In &lt;a href="https://blog.toddbiz.com/blog/2019-06-07-javascript-and-scope-iv/"&gt;JavaScript and Scope IV - Blocks&lt;/a&gt; we showed that blocks on their own do not create a new scope when using the &lt;code&gt;var&lt;/code&gt; keyword. Wouldn't it be nice if there were a way to create the &lt;code&gt;toto&lt;/code&gt; variable inside a block so that it would have its own scope, unlike below?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;toto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;wants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;toto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a puppy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toto&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a puppy'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;ES6 allows block level scope with the &lt;code&gt;let&lt;/code&gt; keyword when declaring variables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;toto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;wants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;toto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a puppy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toto&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 'wants'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/db423zut/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code above, the &lt;code&gt;toto&lt;/code&gt; variable inside the block no longer pollutes the value of the global variable because they are now two different variables even if they have the same name.&lt;/p&gt;

&lt;p&gt;We can also fix the &lt;code&gt;for&lt;/code&gt; loop example from the previous article.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var buzz = 3;

for (let buzz = 1; buzz &amp;lt; 10; buzz++) {
  if (buzz === 9) { 
    console.log('for loop buzz:', buzz); // logs 9
  }
}

console.log('global scope buzz:', buzz); // now logs 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/qr7x2fuk/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we can also fix the &lt;code&gt;if&lt;/code&gt; statement code. This time we use the &lt;code&gt;const&lt;/code&gt; key word so the value cannot be changed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;not&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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;cute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cute&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 'not'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/yjwt75v4/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the examples above, both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; will give you block level scope when you need it. In fact, it's one of the reasons to prefer using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; instead of &lt;code&gt;var&lt;/code&gt;. Variables should have as limited scope as possible to avoid the types of unexpected side effects we saw in the previous article.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript and Scope IV - Blocks</title>
      <dc:creator>Todd Chaffee</dc:creator>
      <pubDate>Wed, 19 Jun 2019 12:34:38 +0000</pubDate>
      <link>https://dev.to/tchaffee/javascript-and-scope-iv-blocks-300</link>
      <guid>https://dev.to/tchaffee/javascript-and-scope-iv-blocks-300</guid>
      <description>&lt;p&gt;In &lt;a href="https://blog.toddbiz.com/tags/javascript-scope-series/"&gt;JavaScript and Scope I, II, and III&lt;/a&gt; we looked at scope and &lt;em&gt;functions&lt;/em&gt;. What about other statements that use blocks? Blocks being sections of code enclosed by curly brackets &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, we often declare variables in a &lt;code&gt;for&lt;/code&gt; loop with a block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;buzz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;buzz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;buzz&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;buzz&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buzz&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;9&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;for loop buzz:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;buzz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 9&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;global scope buzz:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;buzz&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/5m4wk8hu/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you run the code above, you'll see there is only one &lt;code&gt;buzz&lt;/code&gt; variable. The one declared in the global scope. The &lt;code&gt;for&lt;/code&gt; loop changes the global variable. The &lt;code&gt;buzz&lt;/code&gt; variable will have a value of &lt;code&gt;10&lt;/code&gt; when the last &lt;code&gt;console.log&lt;/code&gt; runs.&lt;/p&gt;

&lt;p&gt;What about &lt;code&gt;if&lt;/code&gt; statements?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;not&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cute&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 'cat'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/Lutdkqsx/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, there is only one &lt;code&gt;cute&lt;/code&gt; variable and the final value of the variable in the global scope will be &lt;code&gt;'cat'&lt;/code&gt;. The re-declaration of the variable inside the block does not create a new scope and variable.&lt;/p&gt;

&lt;p&gt;Both of the examples above are the &lt;em&gt;opposite&lt;/em&gt; of what we saw for functions. Functions create a new scope for variables. Blocks do &lt;em&gt;not&lt;/em&gt; create a new scope for variables declared with &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Did you know you can declare blocks all on their own without using an &lt;code&gt;if&lt;/code&gt; or &lt;code&gt;for&lt;/code&gt; statement or anything else? I will leave you with this one final very simple example to help you memorize the rule: &lt;em&gt;blocks do not create a new scope for variables declared with &lt;code&gt;var&lt;/code&gt;&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;toto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;wants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;toto&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a puppy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;toto&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 'a puppy'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/17st5cmk/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript and Scope III - Arrow Functions</title>
      <dc:creator>Todd Chaffee</dc:creator>
      <pubDate>Fri, 14 Jun 2019 14:22:01 +0000</pubDate>
      <link>https://dev.to/tchaffee/javascript-and-scope-iii-arrow-functions-593f</link>
      <guid>https://dev.to/tchaffee/javascript-and-scope-iii-arrow-functions-593f</guid>
      <description>&lt;p&gt;As you saw in the &lt;a href="https://blog.toddbiz.com/blog/2019-06-01-javascript-and-scope/"&gt;JavaScript and Scope I - Intro&lt;/a&gt; and &lt;a href="https://blog.toddbiz.com/blog/2019-06-03-javascript-and-scope-ii/"&gt;JavaScript and Scope II - Functions&lt;/a&gt;, functions create a new scope which will hide or shadow the value of variables in a higher scope, such as global scope.&lt;/p&gt;

&lt;p&gt;What about arrow functions? Let's go back to our first ever code example and change it to an arrow function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bar&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="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// logs 2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Edit and run the code if you don't remember&lt;/span&gt;
&lt;span class="c1"&gt;// what the next line results in.&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="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/xqtwu138/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you run the code above, you'll see that scope behaves the same as a regular function declaration: The &lt;code&gt;foo&lt;/code&gt; variable only exists inside the function scope, and does not exist in the global scope.&lt;/p&gt;

&lt;p&gt;The same is true of all the other examples given so far. If you want to see for yourself and have a few minutes, go ahead and edit every code example from the previous two articles and change them to use only arrow functions. Use the example above as a guide for how to change a regular function declaration to an arrow function. In each case, you will need to declare a variable to give the arrow function a name. Here are all the examples from the last two articles.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/0oy9jc1n/2/"&gt;Example 2, Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/0jwyg6za/"&gt;Example 3, Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/bue5zf9w/"&gt;Example 4, Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/x3f1Ln8e/"&gt;Example 5, Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are a couple of exceptions to how scope works in arrow functions. Arrow function scope does not include its own &lt;code&gt;this&lt;/code&gt; or its own &lt;code&gt;argument&lt;/code&gt; object, which will be covered in another post. For now we are focusing on the scope of declared variables.&lt;/p&gt;

&lt;p&gt;In summary you can count on arrow functions to have their own scope for declared variables, just like regular function declarations.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript and Scope II - Functions</title>
      <dc:creator>Todd Chaffee</dc:creator>
      <pubDate>Fri, 07 Jun 2019 15:19:41 +0000</pubDate>
      <link>https://dev.to/tchaffee/javascript-and-scope-ii-4i4e</link>
      <guid>https://dev.to/tchaffee/javascript-and-scope-ii-4i4e</guid>
      <description>&lt;p&gt;As you saw in the &lt;a href="https://blog.toddbiz.com/blog/2019-06-01-javascript-and-scope/"&gt;previous article about JavaScript scope&lt;/a&gt;, a function can create a new scope which determines the value to use when we have two different variables with the same name.&lt;/p&gt;

&lt;p&gt;But is scope determined by where a function is declared, or where the function is run?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&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;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value of i when bar is declared outside foo: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&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="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/0jwyg6za/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above it should be obvious that &lt;code&gt;i&lt;/code&gt; will have a value of &lt;code&gt;11&lt;/code&gt; when &lt;code&gt;console.log&lt;/code&gt; runs. But what happens if we run &lt;code&gt;bar&lt;/code&gt; inside of &lt;code&gt;foo&lt;/code&gt;? The answer might surprise you if you are new to JavaScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&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;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value of i when bar is declared outside foo: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/bue5zf9w/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;i&lt;/code&gt; variable again will have a value of &lt;code&gt;11&lt;/code&gt; when &lt;code&gt;console.log&lt;/code&gt; runs.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;bar&lt;/code&gt; is declared in the global scope, it changes the value of the &lt;code&gt;i&lt;/code&gt; variable that exists in the same global scope. Even when it runs inside of &lt;code&gt;foo&lt;/code&gt;, it does not reference the &lt;code&gt;i&lt;/code&gt; declared inside of the &lt;code&gt;foo&lt;/code&gt; scope.&lt;/p&gt;

&lt;p&gt;So what happens if we declare the bar function inside of the &lt;code&gt;foo&lt;/code&gt; function?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value of i when bar is declared inside foo: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/x3f1Ln8e/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hopefully you guessed that &lt;code&gt;console.log&lt;/code&gt; will show &lt;code&gt;i&lt;/code&gt; having a value of &lt;code&gt;3&lt;/code&gt;. The &lt;code&gt;bar&lt;/code&gt; function is declared inside the scope of the &lt;code&gt;foo&lt;/code&gt; function, so it will change and output the value of the &lt;code&gt;i&lt;/code&gt; variable declared in the &lt;code&gt;foo&lt;/code&gt; scope.&lt;/p&gt;

&lt;p&gt;Many programming languages work like this, but not all of them so it's good to know the vocabulary for this. When scope is determined by where a function is declared (where it is written in the source code), we call it lexical scope. JavaScript uses lexical scope.&lt;/p&gt;

&lt;p&gt;Learn more about scope in &lt;a href="https://blog.toddbiz.com/blog/2019-06-07-javascript-and-scope-iii/"&gt;JavaScript and Scope III - Arrow Functions&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>JavaScript and Scope I - Intro</title>
      <dc:creator>Todd Chaffee</dc:creator>
      <pubDate>Thu, 06 Jun 2019 20:16:37 +0000</pubDate>
      <link>https://dev.to/tchaffee/javascript-and-scope-i-1e7h</link>
      <guid>https://dev.to/tchaffee/javascript-and-scope-i-1e7h</guid>
      <description>&lt;p&gt;Scope determines where a variable exists and does not exist in a program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;bar&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="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: foo is not defined;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/L4h60y79/6/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Scope also determines which value to use when we have two &lt;em&gt;different&lt;/em&gt; variables with the same name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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;bar&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo inside bar: &lt;/span&gt;&lt;span class="dl"&gt;'&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;// 2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;bar&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo outside bar: &lt;/span&gt;&lt;span class="dl"&gt;'&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;// 1;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/0oy9jc1n/2/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you run the example above, you will first see '2' logged to the console, and then you will see '1'. The &lt;code&gt;foo&lt;/code&gt; created inside the &lt;code&gt;bar&lt;/code&gt; function is a different variable from the &lt;code&gt;foo&lt;/code&gt; created at the top of the program.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;foo&lt;/code&gt; variable declared at the top of the program is in the global scope. The &lt;code&gt;foo&lt;/code&gt; variable declared inside the function is in that function's scope only. We say that the &lt;code&gt;foo&lt;/code&gt; variable declared inside the function &lt;em&gt;shadows&lt;/em&gt; the global variable.&lt;/p&gt;

&lt;p&gt;What about function parameters? Do they shadow variables from a higher level scope?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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;bar&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="p"&gt;{&lt;/span&gt;  &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo inside bar: &lt;/span&gt;&lt;span class="dl"&gt;'&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;// logs 7&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo outside bar: &lt;/span&gt;&lt;span class="dl"&gt;'&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;// logs 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/tchaffee/cqt2dxh4/"&gt;Edit in JSFiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The fact that parameter names act like variables when it comes to scope and shadowing will be important in a future article on scope and closures.&lt;/p&gt;

&lt;p&gt;Learn more about scope in &lt;a href="https://blog.toddbiz.com/blog/2019-06-03-javascript-and-scope-ii/"&gt;JavaScript and Scope II - Functions&lt;/a&gt;&lt;/p&gt;

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