<?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: Tailine</title>
    <description>The latest articles on DEV Community by Tailine (@tailine).</description>
    <link>https://dev.to/tailine</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%2F188975%2F14a1fb3f-3c3d-4158-ae70-5b28e7fea8b0.jpg</url>
      <title>DEV Community: Tailine</title>
      <link>https://dev.to/tailine</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tailine"/>
    <language>en</language>
    <item>
      <title>Scope and Scope chain in Javascript</title>
      <dc:creator>Tailine</dc:creator>
      <pubDate>Wed, 26 May 2021 15:46:57 +0000</pubDate>
      <link>https://dev.to/tailine/scope-and-scope-chain-in-javascript-3478</link>
      <guid>https://dev.to/tailine/scope-and-scope-chain-in-javascript-3478</guid>
      <description>&lt;h2&gt;
  
  
  What is scope?
&lt;/h2&gt;

&lt;p&gt;Scope can be defined as the space in which variables and statements are accessible. It makes it possible to have variables with the same name without colliding with one another and prevents outer scopes from having access to inner scopes.&lt;br&gt;
In Javascript we have three types of scope: &lt;strong&gt;&lt;em&gt;global scope, function/local scope and block scope&lt;/em&gt;&lt;/strong&gt;. Let's learn what those are:&lt;/p&gt;
&lt;h3&gt;
  
  
  Global scope
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The default scope&lt;/li&gt;
&lt;li&gt;There is only one global scope in the program&lt;/li&gt;
&lt;li&gt;It is the top scope that englobes the entire code&lt;/li&gt;
&lt;li&gt;The declarations inside this space can be accessed anywhere in the code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Take a look at the example below. The function &lt;code&gt;getAge&lt;/code&gt; is able to reference the &lt;code&gt;age&lt;/code&gt; that is outside the function but located in the global scope.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Created by a function invocation&lt;/li&gt;
&lt;li&gt;The declarations inside the function are only accessible there&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at another example. Below we are printing the result of &lt;code&gt;getAge&lt;/code&gt; function. The &lt;code&gt;age&lt;/code&gt; constant is located inside the function scope so we are able to return it, but when we try to print it from the global scope we get a reference error. This happens because outer scopes (in this case the global scope) cannot access the inner scope (local scope) created by &lt;code&gt;getAge&lt;/code&gt;.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;The space between a pair of curly braces &lt;em&gt;(if block, for block, etc.)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Applicable to &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Declarations are only accessible inside the block&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the example below we are able to print the variable &lt;code&gt;msgOne&lt;/code&gt; but not constant &lt;code&gt;msgTwo&lt;/code&gt;. As mentioned before &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; are block scoped so they are only visible inside the block, in this case inside the &lt;code&gt;if&lt;/code&gt; statement. On the other hand &lt;code&gt;var&lt;/code&gt; is function scope so can be accessed within the function.&lt;/p&gt;

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

&lt;p&gt;Take a look at the code below. What is printed to the console? The answer is &lt;code&gt;My name is Joana, I'm from Brazil and I speak brazillian portuguese&lt;/code&gt;. But what makes it possible for the &lt;code&gt;displayInfo&lt;/code&gt; function to access the constants from outside your 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;language&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;brazillian portuguese&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Ana&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;displayIntroduction&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Maria&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Brazil&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;displayInfo&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Joana&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`My name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, I'm from &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;country&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; and I speak &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;language&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;return&lt;/span&gt; &lt;span class="nf"&gt;displayInfo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;displayIntroduction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we dive into how, let me briefly talk about the &lt;strong&gt;&lt;em&gt;execution context&lt;/em&gt;&lt;/strong&gt;. This concept won't be covered in this article but it is important to explain what it is in order to make it easier to understand scope chain. So, execution context is an environment in which javascript code is evaluated and executed. When the code first starts running it creates a global execution context and a function execution context is created on each function invocation. The scope chain of this code looks similar to this:&lt;/p&gt;

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

&lt;p&gt;Each execution context has a &lt;strong&gt;&lt;em&gt;scope chain&lt;/em&gt;&lt;/strong&gt;. It consists of the variables and objects referenceable by the execution context. Besides the variables and objects it has a property called &lt;code&gt;outer&lt;/code&gt; that stores the reference to the parent's scope chain.&lt;/p&gt;

&lt;p&gt;When the &lt;code&gt;displayInfo&lt;/code&gt; function is executed and reaches &lt;code&gt;name&lt;/code&gt; it searches for it in the local scope chain, finding &lt;code&gt;Joana&lt;/code&gt; as the value. The same process happens for &lt;code&gt;country&lt;/code&gt; but it is not in the local scope.&lt;/p&gt;

&lt;p&gt;So, how is javascript able to reference it?&lt;/p&gt;

&lt;p&gt;When the constant is not found in the local scope javascript reaches to the parent's local memory accessible by the reference stored in &lt;code&gt;outer&lt;/code&gt;, getting &lt;code&gt;Brazil&lt;/code&gt; as the value.&lt;/p&gt;

&lt;p&gt;Last but not least, the same thing happens to &lt;code&gt;language&lt;/code&gt; in this case the javascript engine goes all the way up the scope chain reaching the global scope to find the constant, retrieving the value &lt;code&gt;brazillian portuguese&lt;/code&gt;. It is important to know that the scope chain works only one way, from the inner scope to the outer scopes, making it impossible for the global execution context to have access to &lt;code&gt;country&lt;/code&gt;, for example.&lt;/p&gt;

&lt;p&gt;Also know that since the global execution context is the top context the &lt;code&gt;outer&lt;/code&gt; points to &lt;code&gt;null&lt;/code&gt;, so if the variable wasn't there it would be implicitly declared, if not in strict mode, or an error would be returned.&lt;/p&gt;

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

&lt;p&gt;Scope and scope chain are fundamental topics to understand how the javascript engine process and executes code.&lt;/p&gt;

&lt;p&gt;To recap:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are three types of scope: global scope, function scope and block scope&lt;/li&gt;
&lt;li&gt;Scopes make possible to have variables with the same name without colliding with each other&lt;/li&gt;
&lt;li&gt;Variables and objects in inner scopes are not accessible from outer scopes&lt;/li&gt;
&lt;li&gt;Scope chain consists of the variables and objects referenceable by the execution context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading :)&lt;/p&gt;

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