<?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>Forem: Kamlesh Chavan</title>
    <description>The latest articles on Forem by Kamlesh Chavan (@kamleshchavan).</description>
    <link>https://forem.com/kamleshchavan</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%2F377071%2Ff4c27084-6584-44be-8a9a-2413d4abd4af.jpeg</url>
      <title>Forem: Kamlesh Chavan</title>
      <link>https://forem.com/kamleshchavan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kamleshchavan"/>
    <language>en</language>
    <item>
      <title>Hoisting in JavaScript</title>
      <dc:creator>Kamlesh Chavan</dc:creator>
      <pubDate>Sun, 03 May 2020 10:57:41 +0000</pubDate>
      <link>https://forem.com/kamleshchavan/hoisting-in-javascript-30ej</link>
      <guid>https://forem.com/kamleshchavan/hoisting-in-javascript-30ej</guid>
      <description>&lt;h3&gt;
  
  
  Hoisting is JavaScript's default behaviour of moving declarations to the top (not literally).
&lt;/h3&gt;

&lt;h4&gt;
  
  
  What is hoisting?
&lt;/h4&gt;

&lt;p&gt;In simple words, hoisting is using variable and function before they are declared. Meaning variables and function declarations moved to the top of their scope before any other code execution, no matter where they are declared. This doesn't mean literally moving your code to top but, the compilers put the declaration into the memory first in the compile phase. The declarations are processed before any other code executed.&lt;/p&gt;

&lt;h5&gt;
  
  
  Undefined:
&lt;/h5&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
In the above example, when we do &lt;code&gt;console.log(name)&lt;/code&gt; at first, we get &lt;code&gt;ReferenceError&lt;/code&gt; because &lt;code&gt;name&lt;/code&gt; is not declared previously. But when we check, &lt;code&gt;console.log(typeof name)&lt;/code&gt; we get &lt;code&gt;undefined&lt;/code&gt; as output. It is because undeclared variables are assigned &lt;code&gt;undefined&lt;/code&gt; at the time of execution. The &lt;code&gt;undefined&lt;/code&gt; is a primitive type in JavaScript, just like String, Boolean, and Numbers. It means you have not explicitly assigned any value to that variable.
&lt;h5&gt;
  
  
  Variables:
&lt;/h5&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Here, we have used the variable &lt;code&gt;firstName&lt;/code&gt; before it is declared. And &lt;code&gt;console.log(firstName)&lt;/code&gt; returns &lt;code&gt;undefined&lt;/code&gt;. This doesn't give &lt;code&gt;ReferenceError&lt;/code&gt;. When we do &lt;code&gt;console.log(firstName)&lt;/code&gt; the second time, we get the expected output. Why? Because JavaScript has hoisted variables. And the code looks like below in the interpreter.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Only function and variable declarations are hoisted in JavaScript&lt;/em&gt;&lt;/strong&gt;. That is the reason we get undefined when we do &lt;code&gt;console.log(firstName)&lt;/code&gt; for the very first time. &lt;strong&gt;&lt;em&gt;Initializations are not hoisted&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h5&gt;
  
  
  Functions
&lt;/h5&gt;

&lt;p&gt;Similarly, function declarations are moved to the top means they are executed before any other code in the program. Check below code snippet.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
It works perfectly fine as if the function &lt;code&gt;getName()&lt;/code&gt; is defined first and then called. This because the declaration is moved to the top. Below is how the compiler reads through the above code.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;h5&gt;
  
  
  ES5 - strict mode
&lt;/h5&gt;

&lt;p&gt;By using the strict mode of JavaScript introduced in es5. We can be more careful and alert about using variable declaration. It helps to prevent us from using undeclared variables.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
When we are using &lt;code&gt;firstName&lt;/code&gt; without declaring, we get an error. Read more about strict mode here.


&lt;h5&gt;
  
  
  ES6 - let and const
&lt;/h5&gt;

&lt;p&gt;ES6 introduced two new ways of declaring variables in JavaScript &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;. Both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; does not support hoisting. Let's see it n practice.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
We get an error when using &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; before the declaration. Learn more about let and const in the below article.&lt;br&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/kamleshchavan" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g1yIvcLN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--G-CUnllI--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/377071/f4c27084-6584-44be-8a9a-2413d4abd4af.jpeg" alt="kamleshchavan"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/kamleshchavan/understanding-let-and-cost-4kad" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Understanding let and const.&lt;/h2&gt;
      &lt;h3&gt;Kamlesh Chavan ・ May 1 '20 ・ 3 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#ecmascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#let&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#const&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;





&lt;p&gt;Let us recollect things we learned up above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declaration of functions and variables are moved to the top of their scope in JavaScript. Meaning compiler executes this declaration before any other code.&lt;/li&gt;
&lt;li&gt;Only declarations are hoisted, not the initializations.&lt;/li&gt;
&lt;li&gt;Using strict mode helps us prevent the use of a variable before the declaration.&lt;/li&gt;
&lt;li&gt;Both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; does not support hoisting.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thank you for reading. 🎉&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Understanding let and const.</title>
      <dc:creator>Kamlesh Chavan</dc:creator>
      <pubDate>Fri, 01 May 2020 09:43:31 +0000</pubDate>
      <link>https://forem.com/kamleshchavan/understanding-let-and-cost-4kad</link>
      <guid>https://forem.com/kamleshchavan/understanding-let-and-cost-4kad</guid>
      <description>&lt;p&gt;ES6 introduced two new ways of declaring variables in JavaScript let and const.&lt;br&gt;
Is there any issue with var? No, 100 thousands line of code is working in production javascript without any problem using var. But its possible to accidentally misuse or create unnecessary weird situations.&lt;/p&gt;

&lt;p&gt;Let's kick things by understanding issues with &lt;code&gt;var&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declaration and value assignments :&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above code, we get &lt;code&gt;Kamlesh&lt;/code&gt; printed first and then &lt;code&gt;Something else&lt;/code&gt; get printed in console as we have reassigned the same variable &lt;code&gt;var name&lt;/code&gt; with a different value. We can go ahead and reassign this &lt;code&gt;var name&lt;/code&gt; to function, number, object, or anything. &lt;br&gt;
We just not only can reassign var based variables but also redeclare them and make new &lt;code&gt;var name = 'something'&lt;/code&gt;. This is perfectly valid with &lt;code&gt;var&lt;/code&gt; based variables, and this can get us in the wired situation like redeclaring the same variables by not knowing we have already used the same variable name.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scope:&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above code, we can see that we have used &lt;code&gt;var firstName&lt;/code&gt; variables both inside and outside the &lt;code&gt;if&lt;/code&gt; block, and &lt;code&gt;console.log(firstName)&lt;/code&gt; are printing results for both without any error. &lt;br&gt;
&lt;code&gt;var&lt;/code&gt; based variables are function scope, meaning they are not available outside the function. Also, they are not block scope variables, meaning they are available outside the block.&lt;/p&gt;



&lt;p&gt;Things with &lt;code&gt;let&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declaration and value assignments :&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In the above code, when we assign value to, &lt;code&gt;name = 'Something'&lt;/code&gt; and we get the desired output in the console. But when we try to redeclare, &lt;code&gt;let&lt;/code&gt; we get an error "already declared". &lt;br&gt;
We can reassign &lt;code&gt;let&lt;/code&gt; but can not redeclare &lt;code&gt;let&lt;/code&gt; based variables. If we try to redeclare &lt;code&gt;let&lt;/code&gt; based variables, we get an error "duplicate declaration".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scope&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here in the above code, you can see when we declare &lt;code&gt;var firstName&lt;/code&gt; inside the block and using it inside block prints the result, but using it outside block is giving an error "variable not defined".&lt;br&gt;
This means that &lt;code&gt;let&lt;/code&gt; variables are block scope variables meaning they are only available inside their declaration scope block.&lt;br&gt;
If we need to use firstName outside the block, we need to declare it up above the if block, and it should work in case of the above example.&lt;/p&gt;



&lt;p&gt;Things with &lt;code&gt;const&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declaration and value assignments :&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here we can see that we can not reassign &lt;code&gt;const&lt;/code&gt; variables, nor can we redeclare them. If we try to do so, we should expect an error. &lt;br&gt;
Point to note here is the &lt;code&gt;const&lt;/code&gt; based variables can not be redefined and also can not be reassigned.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scope&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;const&lt;/code&gt; based variables are also block scope variables, and they are only available with the block they are defined. If we try to access then outside the block, an error is expected.&lt;br&gt;
It's always a good practice to start defining the variable as const and eventually if we determine that some of those values need to be reassigned then we use &lt;code&gt;let&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Many people get confused with const value reassignment. Please see the below code.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can assign an object to const and you can change the value of properties inside the object but can not reassign the value to &lt;code&gt;const person&lt;/code&gt;. There is another example of const &lt;code&gt;someString = 'Dummy string'&lt;/code&gt;, when you try to reassign this &lt;code&gt;someString&lt;/code&gt; variable it gives an error.&lt;/p&gt;




&lt;p&gt;Let's recap what we learned here,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start declaring variables as &lt;code&gt;const&lt;/code&gt; unless their values need to be reassigned. &lt;/li&gt;
&lt;li&gt;So &lt;code&gt;const&lt;/code&gt; first &lt;code&gt;let&lt;/code&gt; if we need to. (let's forget about &lt;code&gt;var&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; can not be redeclared. It gives an error.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; Variables values can be reassigned.&lt;/li&gt;
&lt;li&gt;You can not reassign values to &lt;code&gt;const&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Both &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are block scope. Whereas &lt;code&gt;var&lt;/code&gt; is function scope.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Please read through hoisting in JavaScript. Hoisting is JavaScript's default behaviour of moving declarations to the top (not literally). It is another important concept to know about the variable declaration.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/kamleshchavan" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g1yIvcLN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--G-CUnllI--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/377071/f4c27084-6584-44be-8a9a-2413d4abd4af.jpeg" alt="kamleshchavan"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/kamleshchavan/hoisting-in-javascript-30ej" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Hoisting in JavaScript&lt;/h2&gt;
      &lt;h3&gt;Kamlesh Chavan ・ May 3 '20 ・ 2 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;





&lt;p&gt;Thank you for reading; this is my first blog ever. 🎉&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>ecmascript</category>
      <category>let</category>
      <category>const</category>
    </item>
  </channel>
</rss>
