<?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: sruthiragupathy</title>
    <description>The latest articles on DEV Community by sruthiragupathy (@sruthiragupathy).</description>
    <link>https://dev.to/sruthiragupathy</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%2F347922%2F61427ddb-ea60-46af-a0c3-05ad0408bf25.png</url>
      <title>DEV Community: sruthiragupathy</title>
      <link>https://dev.to/sruthiragupathy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sruthiragupathy"/>
    <language>en</language>
    <item>
      <title>Objects in Javascript</title>
      <dc:creator>sruthiragupathy</dc:creator>
      <pubDate>Sat, 15 Aug 2020 14:51:40 +0000</pubDate>
      <link>https://dev.to/sruthiragupathy/objects-in-javascript-gik</link>
      <guid>https://dev.to/sruthiragupathy/objects-in-javascript-gik</guid>
      <description>&lt;p&gt;Hi there, this post is all about objects. But , First things first if you have not checked out my previous post on &lt;strong&gt;Var vs Let vs Const&lt;/strong&gt; , check it out &lt;a href="https://dev.to/sruthiragupathy/var-vs-let-vs-const-in-javascript-3aeb"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright, Here we go!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an object?
&lt;/h2&gt;

&lt;p&gt;Objects in javascript can be compared to an object in real world. Let us consider a car as an object. The car ofcourse has a lot of characteristics such as brand, color, weight, design-type etc.., &lt;br&gt;
In the example above, car is an object.&lt;br&gt;
let us assume, the brand is BMW , color is black, weight is 1200kgs and the design-type is sedan. &lt;br&gt;
Thus , an object is a collection of properties.The property can be considered as a key-value pair.&lt;br&gt;
The keys in the above case are brand, color, weight and design-type ,while the values are BMW, black, 1200kgs and sedan.&lt;br&gt;
Now let's convert all these to code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j1TB1mtu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mj9qwvwondskrr7x65g8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j1TB1mtu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mj9qwvwondskrr7x65g8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here , the object keys are always &lt;strong&gt;string&lt;/strong&gt; type, while the values can be of any &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures"&gt;type&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing and Setting the object properties
&lt;/h2&gt;

&lt;p&gt;We can access the properties of an object using two ways.&lt;/p&gt;

&lt;h4&gt;
  
  
  Dot Notation
&lt;/h4&gt;

&lt;p&gt;The common way to access the object property is using dot notation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rjVMOJsA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vsnxjy1pyc6731dy6ckf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rjVMOJsA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vsnxjy1pyc6731dy6ckf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Bracket Notation
&lt;/h4&gt;

&lt;p&gt;You can wonder why do we need bracket notation, while we have dot notation. Because I wondered too.&lt;br&gt;
But, this notation is highly useful in two cases ,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;when the object key is not a valid javascript identifier. i.e.., when the property name contains hyphens,space, etc..,&lt;/li&gt;
&lt;li&gt;when the object key is not determined until runtime &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZGUd26ka--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h6kmkr725aj8xecf4zqz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZGUd26ka--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h6kmkr725aj8xecf4zqz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the case 1, Trying to access &lt;strong&gt;screen resolution&lt;/strong&gt; with dot notation gives error since it is not a valid javascript identifier.&lt;br&gt;
In the case 2, the variable &lt;strong&gt;key&lt;/strong&gt; is assigned the value of &lt;strong&gt;"capacity"&lt;/strong&gt;.Trying to access &lt;strong&gt;phone.key&lt;/strong&gt; gives the output &lt;strong&gt;undefined&lt;/strong&gt;,because the object key named &lt;strong&gt;key&lt;/strong&gt; is not present and hence phone[key] gives the result &lt;strong&gt;64 GB&lt;/strong&gt; as the key is assigned the value "capacity" during runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object.keys(),Object.values() and Object.entries() methods
&lt;/h2&gt;

&lt;p&gt;The Object.keys() method returns all the keys of an object as an array.&lt;br&gt;
The Object.values() method returns all the values of an object as an array.&lt;br&gt;
The Object.entries() method returns all the properties(key-value pairs) of an object as an array of key-value pairs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UvBVnpPe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/87ll7vc49tqhtbtdku0k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UvBVnpPe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/87ll7vc49tqhtbtdku0k.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Object Creation
&lt;/h2&gt;

&lt;p&gt;There are different ways in which we can create objects.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Object literals&lt;/li&gt;
&lt;li&gt;New Operator or Constructor&lt;/li&gt;
&lt;li&gt;Object.create method
####Object literals
This is the simplest way of creating objects. The object literal is a list of key-value pairs separated by commas.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1phI4tzO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rjek60ka0gr6j4tmwrpv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1phI4tzO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rjek60ka0gr6j4tmwrpv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Here, car is an object literal&lt;/p&gt;

&lt;h4&gt;
  
  
  New Operator
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WSj2AePz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z9t1s6ybtpsazotswwof.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WSj2AePz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z9t1s6ybtpsazotswwof.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Constructor
&lt;/h4&gt;

&lt;p&gt;This is the generic way of creating objects.If we invoke a function using &lt;strong&gt;new&lt;/strong&gt; operater, the function acts a constructor and returns object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---GZkjYA9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lec4secwlhehdu0h3xmm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---GZkjYA9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lec4secwlhehdu0h3xmm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By this method, we can create any number of objects just by passing values to the constructor function&lt;/p&gt;

&lt;h4&gt;
  
  
  Object.create() method
&lt;/h4&gt;

&lt;p&gt;The Object.create() method can be used when a new object has to be created,using an existing object as the prototype of the newly created object.&lt;br&gt;
Let us consider an object &lt;strong&gt;movies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vpPON9rR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oirz6r8eff39w0bc2eu1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vpPON9rR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oirz6r8eff39w0bc2eu1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the new object movie1 inherits the properties of  existing object &lt;strong&gt;movies&lt;/strong&gt; .&lt;/p&gt;

&lt;p&gt;It is also possible to overwrite the inherited properties.Let us consider a new object &lt;strong&gt;movie2&lt;/strong&gt; which inherits the properties of the existing object &lt;strong&gt;movies&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JheDLukR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9kp6fixnd7ryzfdqpwmt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JheDLukR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9kp6fixnd7ryzfdqpwmt.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the value of the inherited property &lt;strong&gt;Studio&lt;/strong&gt; has been overwritten by &lt;strong&gt;DC&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Writing this helped me understand more about objects, I hope you got a better clarity too:)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>objects</category>
    </item>
    <item>
      <title>Var vs Let vs Const in Javascript</title>
      <dc:creator>sruthiragupathy</dc:creator>
      <pubDate>Sat, 08 Aug 2020 07:18:01 +0000</pubDate>
      <link>https://dev.to/sruthiragupathy/var-vs-let-vs-const-in-javascript-3aeb</link>
      <guid>https://dev.to/sruthiragupathy/var-vs-let-vs-const-in-javascript-3aeb</guid>
      <description>&lt;p&gt;Before speaking about the keywords var,let and const, I believe it is extremely important to understand the concept of scope.&lt;/p&gt;

&lt;h2&gt;
  
  
  Global Scope
&lt;/h2&gt;

&lt;p&gt;Variables declared &lt;strong&gt;outside any function&lt;/strong&gt; (globally) have global scope&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2K55Wjzv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/re2kqbae91b5gmhuc76e.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2K55Wjzv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/re2kqbae91b5gmhuc76e.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the variable global can be accessed from anywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Scope
&lt;/h2&gt;

&lt;p&gt;Variables that are declared &lt;strong&gt;inside a function&lt;/strong&gt; (locally) have function scope&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m_YH3_Fi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6tuadoniffmvk0751k49.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m_YH3_Fi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6tuadoniffmvk0751k49.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the variable local can be accessed only inside the function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Block Scope
&lt;/h2&gt;

&lt;p&gt;Some variables can be accessed only &lt;strong&gt;within block&lt;/strong&gt; i.e.,within {}&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DIplkPHK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/holx3uuqukqr97qdt2cv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DIplkPHK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/holx3uuqukqr97qdt2cv.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here , the variable &lt;strong&gt;block&lt;/strong&gt; can be accessed only within the if block. It cannot be accessed from anywhere else. Thus, the variable &lt;strong&gt;block&lt;/strong&gt; has only block scope.&lt;/p&gt;

&lt;p&gt;Now that we are clear with the scope, let's dive into the &lt;strong&gt;var,let and const&lt;/strong&gt; keywords&lt;/p&gt;

&lt;h2&gt;
  
  
  1.var
&lt;/h2&gt;

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

&lt;p&gt;The variables defined with the keyword &lt;strong&gt;var&lt;/strong&gt; have &lt;strong&gt;function&lt;/strong&gt; scope.If it is defined outside the function , then the scope of the variable is &lt;strong&gt;global&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--314AvQTM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9crm7ueicqhw0lce7t1h.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--314AvQTM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9crm7ueicqhw0lce7t1h.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the variable &lt;strong&gt;foo&lt;/strong&gt; defined with the keyword &lt;strong&gt;var&lt;/strong&gt; can be accessed from anywhere while the variable &lt;strong&gt;bar&lt;/strong&gt; is limited to the function scope.&lt;/p&gt;

&lt;h4&gt;
  
  
  Redeclaration and Reassignment
&lt;/h4&gt;

&lt;p&gt;The variables declared with the keyword &lt;strong&gt;var&lt;/strong&gt; can be redeclared and reassigned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J2-OIn2B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e0uiajxq352srub49rv4.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J2-OIn2B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e0uiajxq352srub49rv4.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.&lt;/p&gt;

&lt;p&gt;Variables declared with &lt;strong&gt;var&lt;/strong&gt; keyword are hoisted at the top of its scope and is initialized with &lt;strong&gt;undefined&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TXJyHz7R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0zdsdjpxyiw3x2quslu4.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TXJyHz7R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0zdsdjpxyiw3x2quslu4.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the variable &lt;strong&gt;foo&lt;/strong&gt; is declared later.But it is hoisted at the top and initialized to undefined.&lt;br&gt;
Only variables declared with &lt;strong&gt;var&lt;/strong&gt; keyword exhibit this behaviour.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---pHuoalR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/46q3si16xb4rmfv3wybe.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---pHuoalR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/46q3si16xb4rmfv3wybe.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, since the variable &lt;strong&gt;a&lt;/strong&gt; is not declared with var keyword, it is not hoisted and hence trying to access the variable before declaration in the hoist function throws an error.&lt;/p&gt;

&lt;h2&gt;
  
  
  2.let
&lt;/h2&gt;

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

&lt;p&gt;The variables declared with the keyword &lt;strong&gt;let&lt;/strong&gt; have &lt;strong&gt;block&lt;/strong&gt; scope.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--deqY5Rri--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h422ufrv9v24ashah8js.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--deqY5Rri--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h422ufrv9v24ashah8js.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the variable &lt;strong&gt;i&lt;/strong&gt; can be accessed only within the &lt;strong&gt;for&lt;/strong&gt; loop. &lt;/p&gt;

&lt;h4&gt;
  
  
  Redeclaration and ReAssignment
&lt;/h4&gt;

&lt;p&gt;Variables declared with &lt;strong&gt;let&lt;/strong&gt; keyword cannot be redeclared, but it can be reassigned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B_FoS-BE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u1fnncsf4tgdby1072ha.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B_FoS-BE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u1fnncsf4tgdby1072ha.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Variables declared with &lt;strong&gt;let&lt;/strong&gt; are also hoisted at the top, but let variables are not initialized until their definition is evaluated.This is because of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let"&gt;Temporal Dead Zone&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NH_pRo_E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/woymgir1uftks1opgb1g.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NH_pRo_E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/woymgir1uftks1opgb1g.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thus,trying to access the variable &lt;strong&gt;foo&lt;/strong&gt; before declaration gives reference error.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.const
&lt;/h2&gt;

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

&lt;p&gt;The variables declared with the &lt;strong&gt;const&lt;/strong&gt; keyword also have block scope.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---jlOByt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/41kylsqvi3p90d5wtfol.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---jlOByt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/41kylsqvi3p90d5wtfol.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Redeclaration and Reassignment
&lt;/h4&gt;

&lt;p&gt;Variables declared with &lt;strong&gt;const&lt;/strong&gt; keyword must be initialized at the time of its declaration itself.Otherwise an error is thrown.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FJNquxuf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cjcfbnuyuedmeq4lxta4.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FJNquxuf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cjcfbnuyuedmeq4lxta4.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Variables declared with &lt;strong&gt;const&lt;/strong&gt; keyword cannot be redeclared and reassigned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xh7P6R8s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uaw1apjhhscqwk6sdgxb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xh7P6R8s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uaw1apjhhscqwk6sdgxb.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Attempting to overwrite objects with const keyword throws error. But object keys are not protected and they are mutable. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iwlbbVPw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tmiadywsbzxiuno62wci.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iwlbbVPw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tmiadywsbzxiuno62wci.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we can see that the object cannot be reassigned, but the keys of the object are mutable.&lt;/p&gt;

&lt;p&gt;Similarly arrays declared with const keyword cannot be overwritten. But the elements in the array is mutable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jXFKrJQB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v0izkbjqolf6t9qc848p.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jXFKrJQB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v0izkbjqolf6t9qc848p.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Variables declared with &lt;strong&gt;const&lt;/strong&gt; keyword behaves the same way as &lt;strong&gt;let&lt;/strong&gt; keyword.They are hoisted at the top of the scope but they are not initialized until their definition is evaluated&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZCeHlh-I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/unu5xz7puc8jdtltjx6u.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZCeHlh-I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/unu5xz7puc8jdtltjx6u.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  var vs let vs const - A Summary
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;var&lt;/th&gt;
&lt;th&gt;let&lt;/th&gt;
&lt;th&gt;const&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Scope&lt;/td&gt;
&lt;td&gt;Function or Global&lt;/td&gt;
&lt;td&gt;Block&lt;/td&gt;
&lt;td&gt;Block&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Redeclare&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reassign&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hoisted&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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