<?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: Muhammad Bilal</title>
    <description>The latest articles on DEV Community by Muhammad Bilal (@bilaltariq).</description>
    <link>https://dev.to/bilaltariq</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%2F187587%2Fd6bd4980-59bd-457e-a5ff-eadf2d76d5ae.jpg</url>
      <title>DEV Community: Muhammad Bilal</title>
      <link>https://dev.to/bilaltariq</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bilaltariq"/>
    <language>en</language>
    <item>
      <title>Variables scope in Javascript</title>
      <dc:creator>Muhammad Bilal</dc:creator>
      <pubDate>Sun, 03 May 2020 13:40:04 +0000</pubDate>
      <link>https://dev.to/bilaltariq/variables-scope-in-javascript-1j6p</link>
      <guid>https://dev.to/bilaltariq/variables-scope-in-javascript-1j6p</guid>
      <description>&lt;p&gt;Javascript variables are a container for storing values. In javascript, there are three types of variables &lt;code&gt;let&lt;/code&gt; &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; variable has a functional scope which means that variables that are defined inside a function can only be accessible within the function.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mOIfrOSS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v1swknn8h48stz73pzy4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mOIfrOSS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v1swknn8h48stz73pzy4.png" alt="var functional scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; type variables don't have block scope they are accessible outside of the block&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w3tX9p0s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p97cw8ynz35i5g77qrxh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w3tX9p0s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p97cw8ynz35i5g77qrxh.png" alt="var block scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var&lt;/code&gt; type variables override &lt;code&gt;var&lt;/code&gt; type variable which has the same name in their scope.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Dsntm_ja--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fsrtqefcxuyr1ouutf4l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Dsntm_ja--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fsrtqefcxuyr1ouutf4l.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redeclaring a &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; variable with &lt;code&gt;var&lt;/code&gt; is not allowed in the same scope and in functional scope &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9Z3x3Iif--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/emxkeu1igj1i00fx5zwi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9Z3x3Iif--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/emxkeu1igj1i00fx5zwi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; type variable has the same functional scope as &lt;code&gt;var&lt;/code&gt; variable but they also have &lt;code&gt;Block sope&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8jwywxbY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zait3hzyk5v6w1b1iw82.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8jwywxbY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zait3hzyk5v6w1b1iw82.png" alt="let functional scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt; type variable defined in &lt;code&gt;Block&lt;/code&gt; are &lt;code&gt;NOT&lt;/code&gt; accessible outside of &lt;code&gt;Block&lt;/code&gt; as shown in the example.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jBsi3DYj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v8lok1txcws4odq8h0zj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jBsi3DYj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v8lok1txcws4odq8h0zj.png" alt="let block scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redeclaring a &lt;code&gt;let&lt;/code&gt; variable with type &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt; is not allowed in the global. The compiler will throw an error&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FA7wd3NR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2096lkfa136qsy1unhyi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FA7wd3NR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2096lkfa136qsy1unhyi.png" alt="let variable global scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redeclaring a &lt;code&gt;let&lt;/code&gt; variable with type &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt; is not allowed in the block. The compiler will throw an error&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--36ykvoeo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lb1161lqzm9g18srdsha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--36ykvoeo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lb1161lqzm9g18srdsha.png" alt="let variable block scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redeclaring a &lt;code&gt;let&lt;/code&gt; variable with type &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt; is not allowed in the functional scope. The compiler will throw an error&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CmT76tDx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/79i5vpncx0j78zbb2aua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CmT76tDx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/79i5vpncx0j78zbb2aua.png" alt="let variable function scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's talk about &lt;code&gt;const&lt;/code&gt; variables. As the name suggests we cannot reassign a value to the &lt;code&gt;const&lt;/code&gt; variable.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IbEoyxcR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k7l00yr6qgz39j1vgf5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IbEoyxcR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k7l00yr6qgz39j1vgf5f.png" alt="const variable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const&lt;/code&gt; type variable also have &lt;code&gt;functional&lt;/code&gt; and &lt;code&gt;Block&lt;/code&gt; level scope.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IzxDnQfB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rdlmm9gtdh9hhnw7bnpi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IzxDnQfB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rdlmm9gtdh9hhnw7bnpi.png" alt="const functional scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const&lt;/code&gt; type variable defined in &lt;code&gt;Block&lt;/code&gt; is &lt;code&gt;NOT&lt;/code&gt; accessible outside of block as shown in the example.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GQurJexx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2p7u2i09v9ukkb2tp266.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GQurJexx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2p7u2i09v9ukkb2tp266.png" alt="const block scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redeclaring a &lt;code&gt;const&lt;/code&gt; variable with type &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt; is not allowed in the global. The compiler will throw an error&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZyWTTncg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mk5ekhywyka9dyz3gkri.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZyWTTncg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mk5ekhywyka9dyz3gkri.png" alt="const variable global scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redeclaring a &lt;code&gt;const&lt;/code&gt; variable with type &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt; is not allowed in the block. The compiler will throw an error&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LpfTg9h1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6azpc5k26z5oq0es7msa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LpfTg9h1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6azpc5k26z5oq0es7msa.png" alt="const variable block scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redeclaring a &lt;code&gt;const&lt;/code&gt; variable with type &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt; is not allowed in the functional scope. The compiler will throw an error&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FvvWzZgh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xyk9lof1gm9lu6jmtmlo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FvvWzZgh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xyk9lof1gm9lu6jmtmlo.png" alt="const variable functional scope"&gt;&lt;/a&gt;&lt;/p&gt;

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