<?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: Keith Charles</title>
    <description>The latest articles on DEV Community by Keith Charles (@indoor_keith).</description>
    <link>https://dev.to/indoor_keith</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%2F492170%2F06bde104-7a1a-4185-b95b-fc86eb0d5077.png</url>
      <title>DEV Community: Keith Charles</title>
      <link>https://dev.to/indoor_keith</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/indoor_keith"/>
    <language>en</language>
    <item>
      <title>Be Selfish, For Once!</title>
      <dc:creator>Keith Charles</dc:creator>
      <pubDate>Mon, 02 Nov 2020 17:00:42 +0000</pubDate>
      <link>https://dev.to/indoor_keith/be-selfish-for-once-1931</link>
      <guid>https://dev.to/indoor_keith/be-selfish-for-once-1931</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This is an ongoing series on how to "finish your projects." Each post dives into a single lesson I've learned that has helped me do better at staying on track and reaching the colloquial finish-line of a project. These lessons are not fool-proof as my &lt;a href="https://dev.to/indoor_keith/we-all-suck-at-finishing-projects-4n6"&gt;first post in the series&lt;/a&gt; will tell you, but they can help with morale and reduce the frequency of dropped projects.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Stop trying to make apps that will ~&lt;em&gt;change the world&lt;/em&gt;~! While the sentiment is heartwarming and commendable, if you're finding it hard to finish your projects, chances are you're putting too much pressure and responsibility on yourself. Not everyone can be Batman.&lt;sup&gt;1&lt;/sup&gt; Just like with those lil yellow party cups on airplanes, you need to put yours on before you can worry about everyone else. You need to get better at completing projects before you can take on the world, my fellow disrupters. &lt;/p&gt;

&lt;p&gt;One great way to make sure you follow through with your projects is to pick up projects that &lt;em&gt;you&lt;/em&gt; would actually use. Think about ways you can you make &lt;em&gt;your&lt;/em&gt; life easier, more convenient, or at least more entertaining. &lt;a href="https://twitter.com/nikkitaftw"&gt;@NikkitaFTW&lt;/a&gt;, developer at &lt;a href="https://twitter.com/codesandbox"&gt;@codesandbox&lt;/a&gt;, made a website that is just a compilation of memes that people made out of photos of her. She did this because she was tired of trying to find them on her own and wanted a one-stop-shop to grab one whenever she wanted to use for whatever. You can find the work of art &lt;a href="https://meme.iamsaravieira.com/"&gt;here&lt;/a&gt;. This wasn't an industry shaking, bleeding edge, ground breaking project. It was literally just something she thought would be hilarious, so she did it! Working on something that's &lt;em&gt;for you&lt;/em&gt; will provide you way more motivation to finish than making another note taker or quiz app. At the very least, you'll be doing everyone a service by not adding another quiz app into the webiverse.&lt;sup&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;So get selfish and make that browser extension that deletes all the "blogginess" out of recipes you find online and gets straight to the ingredients. Or make that text generator that takes whatever you write and converts it to how &lt;a href="https://www.youtube.com/watch?v=2zWOPCi-PWA"&gt;Tommy Wiseau&lt;/a&gt; would say it. Chances are, a lot of people may love what you thought only you would find amusing.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; And why would you even want to be Batman? The guy is sad, &lt;em&gt;all&lt;/em&gt; the time. 😬&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; That's a joke. Quiz apps are great and if you're just getting started with web development or learning a new framework, making a quiz app is a great way to get familiar with a lot of the essentials. Hell, at the time of writing this, I'm in the middle of making a quiz app in Vue.js 🤣&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>productivity</category>
      <category>advice</category>
    </item>
    <item>
      <title>We All Suck At Finishing Projects</title>
      <dc:creator>Keith Charles</dc:creator>
      <pubDate>Sun, 01 Nov 2020 03:19:52 +0000</pubDate>
      <link>https://dev.to/indoor_keith/we-all-suck-at-finishing-projects-4n6</link>
      <guid>https://dev.to/indoor_keith/we-all-suck-at-finishing-projects-4n6</guid>
      <description>&lt;p&gt;We do! &lt;/p&gt;

&lt;p&gt;Go @ some of the biggest names out there in the industry. Ask them how many unfinished projects they have. They couldn't tell you. It's an uncountable number! Jobs are lost, families are made, boredom grows, interests change. Whatever the reason, sometimes it's just unavoidable. My point is, don't get discouraged because of this &lt;em&gt;very normal&lt;/em&gt; thing that happens to everyone throughout their entire career. Just like imposter syndrome, this is something that will follow you forever. It's as common as weekends, and you have to learn to live with this reality.&lt;/p&gt;

&lt;p&gt;Now, I'm not saying you should just give up and accept that you won't ever finish a project in your life, because you will. Out of the sea of ~80% complete app ideas in your private github repos that you'll make in your lifetime, you will look back and revel at the select few you've seen through to the end. Maybe you have one or two already, maybe you don't. The fact of the matter is you'll get there eventually. And when you do, you'll be saying the same thing I'm saying to anyone who needs to hear it.&lt;/p&gt;

&lt;p&gt;I have a few bits of advice that I try and keep in mind whenever starting a new project. They help me stay on track and not lose sight of the finish line. I was going to publish one big post about all of them, but 1) I personally wouldn't want to read all of what I wrote in one sitting, and 2) This sort of advice isn't a "complete guide to" sort of thing. I'm always adding new lessons to my mental tool belt, so rather than editing my dumb-huge post every time I learn something new, I'd rather just make a new post about it. I'm starting with this particular tidbit because before even going into tips on staying on track, everyone needs to understand that unfinished projects are inevitable. So don't feel bad if you start to realize that the project you're currently on is not going to be a project you finish. The faster you can get over being disappointed in yourself, the faster you can get back out there and get coding. ✌&lt;/p&gt;

</description>
      <category>advice</category>
      <category>productivity</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What Got You Into Programming?</title>
      <dc:creator>Keith Charles</dc:creator>
      <pubDate>Sat, 31 Oct 2020 20:33:52 +0000</pubDate>
      <link>https://dev.to/indoor_keith/what-got-you-into-programming-3g02</link>
      <guid>https://dev.to/indoor_keith/what-got-you-into-programming-3g02</guid>
      <description>&lt;p&gt;For me, it was finding out you could add custom templates to your MySpace page by copy/pasting blocks of code into sections of your profile. If you remember, you could make your background look like a matrix screen and your cursor look like it was on fire. The templates I found didn't &lt;em&gt;quite&lt;/em&gt; look the way I wanted them to look, so I started combing through the html and tweaking numbers around until it looked the way I liked. &lt;/p&gt;

&lt;p&gt;I thought it was fun, so I signed up for a web design class in high school. Everything on the curriculum I had already learned from decking out my MySpace profile, so I started learning flash animation using ActionScript during class time.&lt;/p&gt;

&lt;p&gt;That led me to game development, working with UnityScript in Unity, later switching to C#. Then Lua with the Corona and Defold engines. Which got me a job at a game company as a QA Tester. While I was there I started making web tools with my manager using JavaScript. &lt;/p&gt;

&lt;p&gt;Flash forward, I'm now a front-end developer and it's all thanks to MySpace 👍&lt;/p&gt;

</description>
      <category>watercooler</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>All I Know About: Scope in JavaScript</title>
      <dc:creator>Keith Charles</dc:creator>
      <pubDate>Mon, 19 Oct 2020 00:37:03 +0000</pubDate>
      <link>https://dev.to/indoor_keith/all-i-know-about-scope-in-javascript-573c</link>
      <guid>https://dev.to/indoor_keith/all-i-know-about-scope-in-javascript-573c</guid>
      <description>&lt;p&gt;Based off some of the bootcamp students I tutor, "scope" is either not something taught in their curriculum, or at most, briefly touched upon. However, understanding scope plays a huge factor in being able to debug and fix your own code. I'm here to shine a light on scope in JavaScript and why it's such an important concept to understand. If you're someone who understands each line of the code below, but you're unable to tell why it returns an error, you've come to the right place!&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="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;let&lt;/span&gt; &lt;span class="nx"&gt;someVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;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;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;someVar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; ReferenceError: someVar is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Defining Scope
&lt;/h2&gt;

&lt;p&gt;Before discussing the code above, let's actually define scope. Here's what I ripped right out of &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Scope"&gt;MDN's documentation&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Scope - The current context of execution. The context in which values and expressions are "visible" or can be referenced.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Make sense? No? Don't worry, MDN's documentation takes some getting used to and may not make sense when you're just starting out. Let me break down some of the words.&lt;/p&gt;

&lt;p&gt;First, "referencing" a variable. Some of you may understand the difference, but let me quickly explain declaring, defining, and referencing.&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;// this is declaring, but not defining&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myVar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// this is declaring and defining on a single line&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;otherVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// this is referencing a variable that has already been declared&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;otherVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; 10&lt;/span&gt;

&lt;span class="c1"&gt;// this is referencing a previously declared variable&lt;/span&gt;
&lt;span class="c1"&gt;// and defining its value&lt;/span&gt;
&lt;span class="nx"&gt;myVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="c1"&gt;// this is referencing a previously declared variable&lt;/span&gt;
&lt;span class="c1"&gt;// and re-defining its value&lt;/span&gt;
&lt;span class="nx"&gt;otherVar&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; otherVar now equals 30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Referencing a variable is calling a variable that has already been declared before. If you try and reference a variable that hasn't been declared yet, you get an error. Likewise, if you reference a variable that has been declared but hasn't been defined, you'll get an undefined value and no error. Like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myVar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// try to reference a variable that was never declared&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;otherVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; ReferenceError: otherVar is not defined;&lt;/span&gt;

&lt;span class="c1"&gt;//try to reference a variable that WAS declared but never defined&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;myVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the case of context, just think of it as the surrounding rules of how code is read or how a variable is used. Without context, any variable could be read from any part of a js file, or worse, if we have multiple files, a variable could be declared in one file but re-defined or referenced in a different file even if that wasn't our intention. It would be anarchy! 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="c1"&gt;// fileA.js&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// fileB.js&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// fileC.js&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;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; ???&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without any context telling us the rules for each &lt;code&gt;count&lt;/code&gt; there would be no way to tell &lt;code&gt;fileC&lt;/code&gt; which &lt;code&gt;count&lt;/code&gt; to log since we have two &lt;code&gt;count&lt;/code&gt; variables from two different files. And that's what scope is. It's just giving our code some context as to how and where our variables can be referenced. Once we get into the types of scope, this will all start setting in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Scope
&lt;/h2&gt;

&lt;p&gt;JavaScript has a handful of different kinds of scope. One way we can tell our code what kind of scope we want to use is by adding a &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, or &lt;code&gt;const&lt;/code&gt; before the variable name when declaring our variable. This keyword is what tells JavaScript how we want to scope the variable. &lt;/p&gt;

&lt;h3&gt;
  
  
  Block Scope: &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;We'll talk about &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; first since it's considered the new standard after their premiere in ES6 and they're probably what you're using right now anyway. &lt;em&gt;I'll explain what ES6 is in a later post, but for now just know it is a feature release made by the top brass who are hard at work, standardizing JavaScript along with other languages.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; variables use what we call &lt;strong&gt;block scope&lt;/strong&gt;. Anytime you've ever seen curly braces in your code, that represents a block of code. Block scope means that your variable is only readable and writeable within the block it was declared in. This is a perfect time to bring back our problem at the very beginning! Let's look at that again:&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="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;let&lt;/span&gt; &lt;span class="nx"&gt;someVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;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;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;someVar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; ReferenceError: someVar is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how &lt;code&gt;someVar&lt;/code&gt; is declared &lt;em&gt;inside&lt;/em&gt; of the curly braces of the if statement, but we try to call the variable &lt;em&gt;outside&lt;/em&gt; of those curly braces. Block scope tells JavaScript that we only want our variable to exist inside of the block it was declared in. Anything outside of the block will have no &lt;em&gt;reference&lt;/em&gt; to the variable in the block, hence the &lt;code&gt;ReferenceError&lt;/code&gt; we're getting. If we were to move the console log inside of the block, we would be able to log &lt;code&gt;someVar&lt;/code&gt; since it would be within the scope:&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="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;let&lt;/span&gt; &lt;span class="nx"&gt;someVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;someVar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;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;Likewise, if we had child blocks in our block, that is, if we had other blocks inside our block, those children will have access to variables declared in their parent.&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;// parent block of code&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;let&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;orange&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// child block of code inside parent&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="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;color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "orange"&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;No matter how many children or grandchildren the parent block has, the children will always have access to variables declared inside any of their parents, grandparents, etc. However, parent blocks of code cannot reference variables that were declared in one of their children.&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="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="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="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="c1"&gt;// create variable in a child block&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;green&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// try to reference the variable  &lt;/span&gt;
  &lt;span class="c1"&gt;// at a parent block&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;color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; ReferenceError: color is not defined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what if we need to define a variable in a child block, but then reference that variable in a parent block? Let's say you have a function (parent block) and in the function you want to create a variable if some condition is true, but you still have to return the variable at the end of the function. All you have to do is declare the variable in the parent block before the child block:&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;//parent block&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;someFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// declare variable in parent block&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;myVar&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="c1"&gt;// define variable in child block&lt;/span&gt;
    &lt;span class="nx"&gt;myVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;It was true!&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="c1"&gt;// reference variable back in parent block&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;myVar&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;As you can see, even though we &lt;em&gt;defined&lt;/em&gt; &lt;code&gt;myVar&lt;/code&gt; in a child block, we can reference it in the parent block because it was &lt;em&gt;declared&lt;/em&gt; in the parent block.&lt;/p&gt;

&lt;p&gt;You might be wondering what the difference is between &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; is since they both have the same exact scope. While it's true that they both share the same scope, &lt;code&gt;const&lt;/code&gt; variables cannot be mutated from its original definition. For instance:&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;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Keith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;George&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; TypeError: Assignment to constant variable.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whereas let can be changed how ever many times you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Richards&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Urban&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// no errors with this!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps to store data and prevent it from ever being changed, such as storing a url like &lt;code&gt;"http://facebook.com"&lt;/code&gt;. It's pretty safe to assume facebook's url will never change, so to give your code some added security we can store that url in a const variable, and we'll sleep soundly knowing a new line of code won't ever inadvertently change the value of that variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Global Scope: &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, and &lt;code&gt;const&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;When a variable is declared outside of any function or block of code, regardless of if you're using &lt;code&gt;var&lt;/code&gt; &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt;, it is considered &lt;strong&gt;Globally Scoped&lt;/strong&gt;. What this means is that any inner scope has access to reference a globally scoped variable. 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="c1"&gt;// variable declared outside of any function or block&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;iceCream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chocolate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;iceCream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "chocolate"&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="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;iceCream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "chocolate"&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;giveMeIceCream&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;iceCream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "chocolate"&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="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;iceCream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "chocolate"&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;No matter where you are in your code, you will always have access to globally scoped variables. Again, using &lt;code&gt;const&lt;/code&gt; would have the same effect as &lt;code&gt;let&lt;/code&gt;, as would &lt;code&gt;var&lt;/code&gt; in this case. However &lt;code&gt;var&lt;/code&gt; goes a little further, adding your variable as a property of the global &lt;code&gt;window&lt;/code&gt; object. Here's an 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;var&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Keith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "Keith"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the truest form of "Global" as the &lt;code&gt;window&lt;/code&gt; object is always accessible no matter where you are in your file, and no matter what file you're in inside your app/website. &lt;/p&gt;

&lt;h3&gt;
  
  
  Functional/Local Scope: &lt;code&gt;var&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; is the only keyword that creates a &lt;strong&gt;Functional Scope&lt;/strong&gt; also known as &lt;strong&gt;Local Scope&lt;/strong&gt;. That just means that a variable declared inside of a function can be referenced anywhere within that function, regardless of any blocks that may be in the code. Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myFunc&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="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="c1"&gt;// declare variable with var (function scope)&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;someVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bar&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="c1"&gt;// can call any var variable within the same function&lt;/span&gt;
  &lt;span class="c1"&gt;// regardless of block difference&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;someVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "Bar"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// someVar only exists within the function&lt;/span&gt;
&lt;span class="c1"&gt;// it was declared inside of&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;someVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; ReferenceError: someVar is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example, we can see how functional scope differs from block scope. With block scope (if we declared the variable with a &lt;code&gt;let&lt;/code&gt; instead of a &lt;code&gt;var&lt;/code&gt;, the first console log would result in an error because the log is outside of the if statement where the variable is declared, but with functional scope we can access the variable anywhere within &lt;code&gt;myFunc&lt;/code&gt;. As for the other console log outside of &lt;code&gt;myFunc&lt;/code&gt;, we get an error because we're outside of the function, therefore outside of the scope of &lt;code&gt;someVar&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Scopes
&lt;/h2&gt;

&lt;p&gt;Once you've grocked all that we discussed above, we can get into the slightly more complicated versions and aspects of scope in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Module Scope
&lt;/h3&gt;

&lt;p&gt;If you've used JavaScript libraries like React or if you've used ES6 modules where you &lt;code&gt;export&lt;/code&gt; parts of one js file and then &lt;code&gt;import&lt;/code&gt; them into another file, then you've run into &lt;strong&gt;Modular Scope&lt;/strong&gt;. Modular scope prevents code from accessing variables or functions from other files unless you explicitly &lt;code&gt;export&lt;/code&gt; that variable from the file and then &lt;code&gt;import&lt;/code&gt; it to the file you're trying to use it in. Here's an example without modular scope:&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;// fileA.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Keith&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// fileB.js&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;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; ReferenceError: myName is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;fileB&lt;/code&gt; has no idea what &lt;code&gt;myName&lt;/code&gt; is, therefore it cannot log it from within the bounds of its file. However if we were to &lt;code&gt;export&lt;/code&gt; &lt;code&gt;myName&lt;/code&gt; from &lt;code&gt;fileA&lt;/code&gt; then import it to &lt;code&gt;fileB&lt;/code&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;// fileA.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Keith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// fileB.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fileA.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "Keith"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that &lt;code&gt;fileB&lt;/code&gt; knows where to grab &lt;code&gt;myName&lt;/code&gt; from, we can easily access the variable and call it whenever we want from &lt;code&gt;fileB&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lexical/Static Scope
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Lexical scope&lt;/strong&gt; also known as &lt;strong&gt;static scope&lt;/strong&gt; deals with functions within functions, or &lt;em&gt;nested functions&lt;/em&gt;. When you nest functions together the variables inside those functions use the scope that was in place when the functions were first defined. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;someVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm global scoped!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcA&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;someVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm block scoped&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;funcB&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;someVar&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;inner&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;lexicalScope&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;outer&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;someVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "I'm global scoped!"&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;lexicalScope&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;//-&amp;gt; "I'm block scoped"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what the heck is going on here? Let's break it down. we first define &lt;code&gt;someVar&lt;/code&gt; globally. Then we create &lt;code&gt;funcA&lt;/code&gt; and in it, redefine &lt;code&gt;someVar&lt;/code&gt; as a block scoped variable. Next we create &lt;code&gt;funcB&lt;/code&gt; that just logs &lt;code&gt;someVar&lt;/code&gt; which we're grabbing from &lt;code&gt;funcA due to block scoping (&lt;/code&gt;someVar&lt;code&gt;is declared in a parent block so we can access it in a child block). Then we return&lt;/code&gt;funcB&lt;code&gt;at the end of&lt;/code&gt;funcA&lt;code&gt;. Outside of the functions we invoke&lt;/code&gt;funcA&lt;code&gt;and set it inside of our&lt;/code&gt;lexicalScope&lt;code&gt;variable. This will give us&lt;/code&gt;funcB&lt;code&gt;in return. Finally, we console log&lt;/code&gt;someVar&lt;code&gt;which gives us our global variable value. And we console log the invocation of&lt;/code&gt;funcB` which gives us our block scoped variable. &lt;/p&gt;

&lt;p&gt;We're calling &lt;code&gt;funcB&lt;/code&gt; outside of &lt;code&gt;funcA&lt;/code&gt;, so how are we still accessing the &lt;code&gt;someVar&lt;/code&gt; inside of &lt;code&gt;funcA&lt;/code&gt;? Well, I'll reiterate: When you nest functions together the variables inside those functions use the scope that was in place when the functions were first defined. When &lt;code&gt;funcB&lt;/code&gt; was first defined, the scope of &lt;code&gt;someVar&lt;/code&gt; was block scoped because of the variable we declared in &lt;code&gt;funcA&lt;/code&gt; which was the parent block of &lt;code&gt;funcB&lt;/code&gt;. Therefore, whenever we call that nested inner function, &lt;code&gt;funcB&lt;/code&gt;, we grab the variable it referenced when it was first defined, not the globally scoped variable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping it up
&lt;/h2&gt;

&lt;p&gt;As you can see, there are a number of different scopes to keep in mind when coding in JavaScript. Don't worry if you need to come back to this as a reference from time to time! It will take a minute before you get a full grasp of every kind of scope JavaScript touts. Just keep an eye on &lt;em&gt;where&lt;/em&gt; you're declaring your variables, and remember what scope the keyword you're using encompasses. (But you should really be using &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; at this point!) ✌&lt;/p&gt;

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