<?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: Yash Kalaria</title>
    <description>The latest articles on DEV Community by Yash Kalaria (@klaus).</description>
    <link>https://dev.to/klaus</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%2F288308%2Fd5126aa8-b488-4a71-afe5-15f291bd4ce4.jpg</url>
      <title>DEV Community: Yash Kalaria</title>
      <link>https://dev.to/klaus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/klaus"/>
    <language>en</language>
    <item>
      <title>var, let and const : ✨demystified✨</title>
      <dc:creator>Yash Kalaria</dc:creator>
      <pubDate>Fri, 25 Mar 2022 03:48:06 +0000</pubDate>
      <link>https://dev.to/klaus/var-let-and-const-demystified-35fl</link>
      <guid>https://dev.to/klaus/var-let-and-const-demystified-35fl</guid>
      <description>&lt;p&gt;Hello folks. Welcome to my another post.&lt;/p&gt;

&lt;p&gt;In this post, we will learn about &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; from the basics to the magical parts of it. So please bear with me till the end.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variables
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Variables in programming languages are means to store information in memory and assign a human-readable label to it for future references.&lt;/li&gt;
&lt;li&gt;There are a few rules for variable naming:

&lt;ul&gt;
&lt;li&gt;The name must contain only letters, digits, or the symbols $ and _&lt;/li&gt;
&lt;li&gt;The first character must not be a digit&lt;/li&gt;
&lt;li&gt;It can't contain any whitespace characters&lt;/li&gt;
&lt;li&gt;There are some &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords" rel="noopener noreferrer"&gt;reserved&lt;/a&gt; words that can't be used as variable names&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;In Javascript, there are two types of variables: Primitives and Reference types. Boolean, string and number are examples of primitive types while objects and arrays are examples of reference type. &lt;/li&gt;

&lt;li&gt;Javascript is a dynamically typed language. That means we can assign different types to different variables without having an error(for &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;let&lt;/code&gt; keywords and not &lt;code&gt;const&lt;/code&gt;)&lt;/li&gt;

&lt;li&gt;In Javascript, we can declare a variable using &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;.&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Some of the terms that I will be using in this post
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Scope
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Scope in Javascript refers to the variable's accessibility in the code. Based on the scope of a variable, some variables can be accessed in some part of the code while some can't be accessed in that part of the code.&lt;/li&gt;
&lt;li&gt;There are three types of scopes: Global, Function and Block.

&lt;ul&gt;
&lt;li&gt;Variables declared at the top level(outside any function) are global scoped. They can be accessed throughout the program.&lt;/li&gt;
&lt;li&gt;Variables declared inside a function are function scoped and can only be accessed inside that function. It will throw a reference error if tried to access outside the function.&lt;/li&gt;
&lt;li&gt;Variables declared inside &lt;code&gt;{}&lt;/code&gt; are called block scoped and their accessibility depends on the keyword that was used to declare them(from &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Scope chain
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Javascript creates scopes for every executing function and &lt;code&gt;{}&lt;/code&gt; block. There is also a global scope that holds some special values and variables that are in the global scope. &lt;/li&gt;
&lt;li&gt;Each scope has access to the parent scope in which it is defined. By using it, the current scope can access the variables from the parent scope. This creates a chain of scope which is called a scope chain.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Hoisting
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, before execution of the code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I won't be going deep into any of these topics right now. (Maybe in the future posts 😉)&lt;/p&gt;




&lt;p&gt;Now let's learn about &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  var
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;var&lt;/code&gt; keyword is an old way of creating variables in Javascript.&lt;/li&gt;
&lt;li&gt;Javascript engine doesn't throw an error if we try to create two variables of the same name in the same scope using &lt;code&gt;var&lt;/code&gt;. If the second statement is an assignment then it will replace the value within the variable. If the second statement is just a declaration then it will be ignored. Javascript engine won't throw an error here.
&lt;/li&gt;
&lt;/ul&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&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;test&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hello&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nice&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Nice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The scope of a variable declared with &lt;code&gt;var&lt;/code&gt; is its current execution context and closures(Maybe in the future post). In simpler words, &lt;code&gt;var&lt;/code&gt; declarations are function scoped and accessible inside that function and variables that are declared in the global scope are accessible anywhere.
&lt;/li&gt;
&lt;/ul&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="nf"&gt;testFn&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;test1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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;test2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nice&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;test2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hello Nice&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;innerFn&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;test3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wow&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;test2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hello Nice&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// test3 is not accessible here.&lt;/span&gt;
  &lt;span class="c1"&gt;// It will throw a ReferenceError.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;testFn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// test1, test2 and test3 are not accessible here. &lt;/span&gt;
&lt;span class="c1"&gt;// They will throw a ReferenceError.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When using &lt;code&gt;var&lt;/code&gt;, we can modify or re-assign any type of primitive values or reference values.
&lt;/li&gt;
&lt;/ul&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nice&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Nice&lt;/span&gt;
&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wow&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["wow"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; declarations are hoisted and initialized with the value &lt;code&gt;undefined&lt;/code&gt;. What this means is that we can use a variable before it is declared but it won't have any value till any one of the assignment statement gets executed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="c1"&gt;// It didn't throw an error 🙂&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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;h3&gt;
  
  
  let
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; keyword is used for creating block scoped variables.&lt;/li&gt;
&lt;li&gt;Unlike &lt;code&gt;var&lt;/code&gt;, we can't have two variable declarations using &lt;code&gt;let&lt;/code&gt; with the same name inside the same scope. It will throw an error.
&lt;/li&gt;
&lt;/ul&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bad&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// SyntaxError: Identifier 'test' has already been declared&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The scope of a variable declared with &lt;code&gt;let&lt;/code&gt; is the curly brackets containing the variable and for the global scope, it is accessible after the declaration throughout the program.
&lt;/li&gt;
&lt;/ul&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hello&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: test is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; declarations are also hoisted but not initialized. That means accessing a variable before its declaration will throw an error.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: test is not defined&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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;ul&gt;
&lt;li&gt;Same as &lt;code&gt;var&lt;/code&gt;, when using &lt;code&gt;let&lt;/code&gt;, we can modify or re-assign any type of primitive values or reference values.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  const
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are the same. The only difference is in the modification and re-assignment of the variable.&lt;/li&gt;
&lt;li&gt;All the variables declared using &lt;code&gt;const&lt;/code&gt; and having a primitive value can't be modified or re-assigned. It will throw an error if tried to do so.
&lt;/li&gt;
&lt;/ul&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// TypeError: Assignment to constant variable.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;All the variables declared using &lt;code&gt;const&lt;/code&gt; and having a reference type value, can be modified but can't be re-assigned.
&lt;/li&gt;
&lt;/ul&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;World&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ["Hello", "World"]&lt;/span&gt;
&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// TypeError: Assignment to constant variable.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Now let's demystify some of the magical cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 1
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's try to assign a value to a variable before its declaration with &lt;code&gt;let&lt;/code&gt;(or&lt;code&gt;const&lt;/code&gt;) and see what happens.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bad&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// ReferenceError: Cannot access 'test' before initialization&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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;ul&gt;
&lt;li&gt;As expected, this gives an error. But a lot is going on here and let's try to understand it.&lt;/li&gt;
&lt;li&gt;Here &lt;code&gt;test&lt;/code&gt; is declared using &lt;code&gt;let&lt;/code&gt;, so it will get hoisted, but it won't get initialized. Since it doesn't get initialized, trying to assign it a value will give an error "Cannot access 'test' before initialization".&lt;/li&gt;
&lt;li&gt;Now let's try to do the same thing with &lt;code&gt;var&lt;/code&gt; and see what happens.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wow&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Wow&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Hello &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here &lt;code&gt;var&lt;/code&gt; declaration is first hoisted and then initialized with the &lt;code&gt;undefined&lt;/code&gt; value which is why the first console will print &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then as the variable is initialized assigning a value &lt;code&gt;Wow&lt;/code&gt; to it works fine and the second console prints &lt;code&gt;Wow&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When the Javascript engine comes to the &lt;code&gt;let&lt;/code&gt; declaration it simply assigns the value &lt;code&gt;Hello&lt;/code&gt; to it and that is why the third console prints &lt;code&gt;Hello&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Case 2
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's see an interesting case with hoisting and variable shadowing.
&lt;/li&gt;
&lt;/ul&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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;let&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wow&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Will this throw an error???&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="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Will this execute???&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Let's try to dissect it.&lt;/li&gt;
&lt;li&gt;Here we have declared a variable named &lt;code&gt;test&lt;/code&gt; and initialized it with the value &lt;code&gt;Hello&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then when it enters the &lt;code&gt;if&lt;/code&gt; block, it will create a new scope. As always, Javascript will hoist the declaration of the &lt;code&gt;test&lt;/code&gt; variable and it won't get initialized as it is declared using &lt;code&gt;let&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then the Javascript engine will assign it the value &lt;code&gt;Wow&lt;/code&gt;. It will work as the &lt;code&gt;let&lt;/code&gt; is block scoped and Javascript can have the same named variables in different scopes.&lt;/li&gt;
&lt;li&gt;Now when we reach the console Javascript engine will try to find the variable in the current scope and as the current scope has the variable with the name &lt;code&gt;test&lt;/code&gt; it will use it and it won't use the variable from the parent scope. This is called variable shadowing.&lt;/li&gt;
&lt;li&gt;As the inner variable's scope is over with &lt;code&gt;if&lt;/code&gt;'s curly brackets, the last console will print &lt;code&gt;Hello&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Let's look at an example with a small variation.
&lt;/li&gt;
&lt;/ul&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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="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="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 🤔&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wow&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here when the Javascript engine enters the &lt;code&gt;if&lt;/code&gt; block, it will create a new scope. As always Javascript engine will hoist the declaration of the &lt;code&gt;test&lt;/code&gt; variable and it won't be initialized as it is declared using &lt;code&gt;let&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;So as we can guess now there is a variable with an uninitialized state in the current scope so Javascript won't use the parent value and throw &lt;code&gt;ReferenceError: Cannot access 'test' before initialization&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now let's look at the same example using &lt;code&gt;var&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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="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="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 🤔&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wow&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here when the Javascript engine enters the &lt;code&gt;if&lt;/code&gt; block, it will create a new scope. As always Javascript will try to hoist the declaration of the &lt;code&gt;test&lt;/code&gt; variable but the variables declared using &lt;code&gt;var&lt;/code&gt; are not block scoped, they are function scoped.&lt;/li&gt;
&lt;li&gt;Javascript engine will not hoist it as a same named variable is already there in the current scope. So the first console will use the value from the parent which is &lt;code&gt;Hello&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When the engine reaches the declaration of the &lt;code&gt;test&lt;/code&gt; variable inside the &lt;code&gt;if&lt;/code&gt; block it is treated as the declaration of the same named variable as the &lt;code&gt;var&lt;/code&gt; is function scoped and the engine will simply assign the value &lt;code&gt;Wow&lt;/code&gt; to the &lt;code&gt;test&lt;/code&gt; variable and the second console will print &lt;code&gt;Wow&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;As the parent variable is re-assigned with the new value, the third console will also print &lt;code&gt;Wow&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Bear with me there is more 😁&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 3
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's look at an interesting case of &lt;code&gt;var&lt;/code&gt; inside the &lt;code&gt;if&lt;/code&gt; block.
&lt;/li&gt;
&lt;/ul&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;false&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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Reference error??? 🤔&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here as we can see that the if block doesn't get executed as the condition, is false, so it should throw a Reference error. Right? Right???&lt;/li&gt;
&lt;li&gt;Well here it won't throw a Reference error and instead, it prints &lt;code&gt;undefined&lt;/code&gt; 🙂.&lt;/li&gt;
&lt;li&gt;The reason for this is that the Javascript engine still hoists the &lt;code&gt;test&lt;/code&gt; variable even if this code doesn't get executed and our global scope is now polluted with an extra unnecessary variable. One of the reasons why you should avoid using &lt;code&gt;var&lt;/code&gt; 😅.&lt;/li&gt;
&lt;li&gt;In the older code you may see an interesting pattern called IIFE - Immediately Invoked Function Expression through which people avoided the scope pollution.
&lt;/li&gt;
&lt;/ul&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;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// or true&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &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;test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// Some code that uses test&lt;/span&gt;
  &lt;span class="p"&gt;})();&lt;/span&gt; &lt;span class="c1"&gt;// Note the invocation here&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ReferenceError: test is not defined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we have created an anonymous function and immediately called it. Javascript treats it as an expression(thus IIFE). &lt;/li&gt;
&lt;li&gt;As we know that the &lt;code&gt;var&lt;/code&gt; is function scoped and thus it can't be accessed outside the anonymous function.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Case 4
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's look at some of the weird cases of the variables declared using &lt;code&gt;var&lt;/code&gt; in the case of &lt;code&gt;for&lt;/code&gt; loops. Let's start with a simple example.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Do something&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="nf"&gt;log&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="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;As we can see here that the console prints the value &lt;code&gt;3&lt;/code&gt; and that is because the variables declared using &lt;code&gt;var&lt;/code&gt; are function or global scoped and not block scoped. So here &lt;code&gt;i&lt;/code&gt; is accessible even after the &lt;code&gt;for&lt;/code&gt; loop. Again scope pollution 🙂.&lt;/li&gt;
&lt;li&gt;Let's look at another famous &lt;code&gt;for&lt;/code&gt; loop problem with &lt;code&gt;var&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&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;fnArray&lt;/span&gt; &lt;span class="o"&gt;=&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fnArray&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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="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;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&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="nx"&gt;fnArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// 0, 1 and 2 ??? 🙂&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we may think that it should print &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt; but it won't and let me tell you why.&lt;/li&gt;
&lt;li&gt;Here we have created an array named fnArray and we have pushed some functions in it which is using the variable &lt;code&gt;i&lt;/code&gt; from the &lt;code&gt;for&lt;/code&gt; loop.&lt;/li&gt;
&lt;li&gt;We know that &lt;code&gt;var&lt;/code&gt; is function scoped so its accessibility doesn't have to do anything with the &lt;code&gt;for&lt;/code&gt; loop. The function is using the variable &lt;code&gt;i&lt;/code&gt; but it will only access its value when it is being executed.&lt;/li&gt;
&lt;li&gt;In the last iteration of the first &lt;code&gt;for&lt;/code&gt; loop, &lt;code&gt;i++&lt;/code&gt; will be executed with the value &lt;code&gt;2&lt;/code&gt; and it will become &lt;code&gt;3&lt;/code&gt; which will stop the loop. Now variable &lt;code&gt;i&lt;/code&gt; will be accessible outside the &lt;code&gt;for&lt;/code&gt; loop with value &lt;code&gt;3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now when the second &lt;code&gt;for&lt;/code&gt; loop gets executed, it will call the anonymous function which will try to console the value of the variable &lt;code&gt;i&lt;/code&gt; and as the value of &lt;code&gt;i&lt;/code&gt; is now &lt;code&gt;3&lt;/code&gt; it will print &lt;code&gt;3&lt;/code&gt; three times.&lt;/li&gt;
&lt;li&gt;This problem can be solved easily by using &lt;code&gt;let&lt;/code&gt; in the first &lt;code&gt;for&lt;/code&gt; loop.
&lt;/li&gt;
&lt;/ul&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;fnArray&lt;/span&gt; &lt;span class="o"&gt;=&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;let&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;0&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;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&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="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;fnArray&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="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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="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;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&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="nx"&gt;fnArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// 0, 1 and 2 as expected&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;This will work because the &lt;code&gt;let&lt;/code&gt; variables are block scoped. So each iteration of the &lt;code&gt;for&lt;/code&gt; loop will create a scope and it will hold the value of &lt;code&gt;i&lt;/code&gt; for that iteration.&lt;/li&gt;
&lt;li&gt;So when the function will try to access the value of &lt;code&gt;i&lt;/code&gt;, it will see the correct value in the scope created by the &lt;code&gt;for&lt;/code&gt; loop and print &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt; as expected.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Summary
&lt;/h3&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%2Frukmywpfanoqy4iy4g0a.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%2Frukmywpfanoqy4iy4g0a.png" alt="Summary of var, let and const"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;So that's it for today folks 😅. &lt;br&gt;
Thanks for bearing with me till the end. Give the post a Heart if you liked the post and give a comment or ping me in case I have missed anything.&lt;/p&gt;




&lt;p&gt;You can reach me on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/yash-kalaria-50388b131" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/PRO-KlauS" rel="noopener noreferrer"&gt;Github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Email : &lt;a href="mailto:yash.kalaria93@gmail.com"&gt;yash.kalaria93@gmail.com&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>[2021] Setting up Husky pre-commit hook with ESLint, Prettier and lint-staged for React and React Native.</title>
      <dc:creator>Yash Kalaria</dc:creator>
      <pubDate>Mon, 09 Dec 2019 09:55:52 +0000</pubDate>
      <link>https://dev.to/botreetechnologies/setting-up-husky-pre-commit-hook-with-eslint-prettier-and-lint-staged-for-react-and-react-native-d05</link>
      <guid>https://dev.to/botreetechnologies/setting-up-husky-pre-commit-hook-with-eslint-prettier-and-lint-staged-for-react-and-react-native-d05</guid>
      <description>&lt;p&gt;Hello folks. I hope you all are doing well.&lt;br&gt;
In this post, we will talk about how you can setup &lt;a href="https://github.com/typicode/husky"&gt;Husky&lt;/a&gt; pre-commit hook with &lt;a href="https://eslint.org/"&gt;ESLint&lt;/a&gt;, &lt;a href="https://github.com/prettier/prettier"&gt;Prettier&lt;/a&gt; and &lt;a href="https://github.com/okonet/lint-staged"&gt;lint-staged&lt;/a&gt; to avoid bad commits and properly format code before committing. So let's get into it.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is Husky?
&lt;/h3&gt;

&lt;p&gt;Husky lets us run commands or script before committing or pushing our code to git. There are a lot of other use cases of Husky too but we will only be using pre-commit hook for this article.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is ESLint?
&lt;/h3&gt;

&lt;p&gt;ESLint is a tool that can analyze our code and find errors in that code using ESLint rules. It can also fix some errors on its own.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is Prettier?
&lt;/h3&gt;

&lt;p&gt;Prettier is an opinionated code formatter that can format our code with the help of rules that you set or defaults are used.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is lint-staged?
&lt;/h3&gt;

&lt;p&gt;lint-staged can run multiple linters against staged git files which in our case are ESLint and Pretttier.&lt;/p&gt;
&lt;h3&gt;
  
  
  Setup new React or &lt;a href="https://www.botreetechnologies.com/react-native-development"&gt;React Native project&lt;/a&gt;.
&lt;/h3&gt;

&lt;p&gt;For React use &lt;br&gt;
&lt;code&gt;npx create-react-app demo&lt;/code&gt;&lt;br&gt;
&lt;code&gt;cd demo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For React Native use &lt;br&gt;
&lt;code&gt;npx react-native init demo&lt;/code&gt;&lt;br&gt;
&lt;code&gt;cd demo&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Let's install some necessary libraries.
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;npm i -D husky lint-staged eslint-config-airbnb prettier&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here we are saving these modules as devDependencies to specify that these are only required in the development and not in runtime. I am using &lt;a href="https://github.com/airbnb/javascript"&gt;Airbnb&lt;/a&gt;'s pre config file for ESLint. You can also go plain if you want to setup all ESLint rules by your self or you can also extend multiple pre configs like this.&lt;/p&gt;
&lt;h3&gt;
  
  
  Setting up ESLint
&lt;/h3&gt;

&lt;p&gt;Replace or create .eslintrc.js file with the following code. If you have created a React project then remove "eslintConfig" from the package.json file as we have created a separate configuration file for the ESLint.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
It is advisable to extend "react-app" for React projects and "@react-native-community" for React Native projects. These files are automatically installed so you don't need to worry about them. Just append this to the start of the array in the extend option in the file.
&lt;h3&gt;
  
  
  Setting up Prettier
&lt;/h3&gt;

&lt;p&gt;Replace or create .prettierrc.js file with the following code.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Setting up Husky pre-commit hook and lint-staged
&lt;/h3&gt;

&lt;p&gt;In the latest versions of Husky we need to enable Git Hooks and then create and add the pre-commit hook. To do that run the following commands in the terminal.&lt;br&gt;
&lt;code&gt;npx husky install&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npx husky add .husky/pre-commit "npx --no-install lint-staged"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Make sure to commit the auto-generated husky folder to your Git repo.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The above code will run lint-staged command against the staged files before committing. &lt;strong&gt;Make sure to run &lt;code&gt;npx husky install&lt;/code&gt; if you clone your Husky configured git repo.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;So now let's add the lint-staged in our package.json file.&lt;/p&gt;

&lt;p&gt;Open your package.json file and add the code that I have specified at the same level as dependencies.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The above code will run Prettier and ESLint rules against all js,jsx,ts,tsx staged files. You can also specify more file extensions or you can write a script for a specific folder you want.&lt;/p&gt;

&lt;h3&gt;
  
  
  Read Also: &lt;a href="https://www.botreetechnologies.com/blog/how-react-native-improves-developer-productivity"&gt;How React Native Improves Developer Productivity&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;So yeah that's it. Now when you will try to commit any changes, ESLint and Prettier rules will execute and it will format your code and give you errors if there are any and stop you from causing any bad commits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: If you get dependency errors for &lt;code&gt;eslint-plugin-jsx-a11y&lt;/code&gt; or &lt;code&gt;eslint-plugin-import&lt;/code&gt; while commiting, then also install them as devDependencies.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At &lt;a href="https://www.botreetechnologies.com/"&gt;BoTree Technologies&lt;/a&gt;, we build web and mobile applications to add value to our client’s business. We align ourselves to ensure that our client benefits the most out of our engagement.&lt;/p&gt;

&lt;p&gt;We work in &lt;a href="https://www.botreetechnologies.com/ruby-on-rails"&gt;Ruby on Rails&lt;/a&gt;, &lt;a href="https://www.botreetechnologies.com/python"&gt;Python&lt;/a&gt;, Java, &lt;a href="https://www.botreetechnologies.com/react-native-development"&gt;React&lt;/a&gt;, Android, iOS and &lt;a href="https://www.botreetechnologies.com/robotic-process-automation"&gt;RPA&lt;/a&gt; as well.&lt;/p&gt;

&lt;p&gt;Drop us a line to discuss how can we help take your business to the next level. Reach out to learn more about the &lt;a href="https://www.botreetechnologies.com/usa/web-development-company-new-york"&gt;software development companies in NYC&lt;/a&gt; for the various ways to improve or build the quality of projects and across your company.&lt;/p&gt;

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