<?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: Naveen710</title>
    <description>The latest articles on DEV Community by Naveen710 (@naveeneggadi).</description>
    <link>https://dev.to/naveeneggadi</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%2F928938%2Fff07dbe7-3cbc-436f-8182-e731284d1619.png</url>
      <title>DEV Community: Naveen710</title>
      <link>https://dev.to/naveeneggadi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/naveeneggadi"/>
    <language>en</language>
    <item>
      <title>Temporal dead zone</title>
      <dc:creator>Naveen710</dc:creator>
      <pubDate>Tue, 20 Sep 2022 19:02:49 +0000</pubDate>
      <link>https://dev.to/naveeneggadi/temporal-dead-zone-1cam</link>
      <guid>https://dev.to/naveeneggadi/temporal-dead-zone-1cam</guid>
      <description>&lt;p&gt;what is the temporal dead zone?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Temporal Dead Zone is the time period between which a variable is inaccessible, to the point where it is initialized&lt;/strong&gt;. To know it better, we first need to dive a bit into a concept called hoisting&lt;/p&gt;

&lt;h2&gt;
  
  
  HOISTING
&lt;/h2&gt;

&lt;p&gt;In JavaScript all the variables, let it be initialized with let , var or const are hoisted. Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code. So every variable declared anywhere in the code is moved to the top of the scope by the interpreter before execution of actual code even begins. now, hoisting is different for var and let, const.&lt;/p&gt;

&lt;p&gt;In var, the variable gets hoisted in the global object and no TDZ is faced but in let and const the variable is hoisted in script scope and given a memory location that cannot be accessed before initializing the variable. this causes TDZ in let and const.&lt;/p&gt;

&lt;p&gt;example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a);  // here 'undefined' will be consoled as no TDZ occurs in var
var a=3;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a);// here TDZ occurs and Reference error is thrown
console.log(b);//gets undefined 
let a=3;
var b=5; // now a is initialized
console.log(a); // No TDZ .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since in the second example we declared let after the calling so TDZ occurred. A reference error is thrown for the same as we are using the variable before it is initialized and var don't throw any error as we are calling it even before initializing it that is because of hoisting&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Var, let and const in Javascript</title>
      <dc:creator>Naveen710</dc:creator>
      <pubDate>Mon, 19 Sep 2022 16:39:29 +0000</pubDate>
      <link>https://dev.to/naveeneggadi/hoisting-in-javascript-32mj</link>
      <guid>https://dev.to/naveeneggadi/hoisting-in-javascript-32mj</guid>
      <description>&lt;p&gt;While learning javascript, it is important to understand how const, let, and var act. These keywords are used to declare variables in javascript. While understanding these keywords, it is also important to understand the concepts of scope and hoisting in javascript.&lt;/p&gt;

&lt;h2&gt;
  
  
  var keyword :
&lt;/h2&gt;

&lt;p&gt;When we declare a variable using var keyword, it can be function scoped or global scope. If we declare var outside the function then it is globally scoped and can be accessed anywhere in the code. If var is declared inside a function, then it is function scoped.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 2;     //varable declared gloabally

function localScope() {
   var b = 5;
   console.log(a);   // output 2
   console.log(b);   // output 5
}
localScope();
console.log(a);    // output 2
console.log(b);    // ReferenceError :  b is not defined

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, when we are accessing 'a' inside the function localScope, it works well. Here, 'a' is declared gloablly scoped. But when we are accessing 'b' outside the localScope function, it gives an error because the variable b is declared locally inside a function&lt;/p&gt;

&lt;p&gt;We can reassign some new value to variable which is declared with var or we can also redeclare. The variable holds the last assigned value and both the cases work well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 2;

a = 2;     //  reinitiated with some other value
console.log(a)    // output 2

var a = 20;     // redeclared
console.log(a);   // output 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the variable even before it is initiated this will show variable is "undefined". This is because of hoisting in javascript which will be discussed soon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a)    //undefined
var a=100;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  let keyword :
&lt;/h2&gt;

&lt;p&gt;The let keyword is block scoped which means variables which declared as let can be accessed only in the block in which it is initialized. If we try to access it outside the block, it gives an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  let a = 10;
}
console.log(a);   // ReferenceError : a not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can reinitialize value to a variable that is declared using let but we cannot redeclare it. If we use variable before it is declared, it gives an error whereas in case of var, it says 'undefined'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 10;   // declared

a = 20;    // reinitialized works fine

let a = 30;     // SyntaxError: Identifier 'a' has already been declared
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  const keyword :
&lt;/h2&gt;

&lt;p&gt;const as the name suggests we can't change the value which is assigned to const variable. Once the value is initialized to a variable, it cannot be redeclared or reinitialized. The variables declared using 'let' are block scoped and variables cannot be used before its declaration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const x = 10;

x = 20   // TypeError : Assigning to a constant variable.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In case of objects, we can add properties to the object assigned but we cannot change whole object. Same in case of arrays too. We can add values to existing array but cannot assign new array to same variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const details = {
  name: "Naveen",
  age : 21,
};
console.log(details)   // { name: 'Naveen', age: 21 }

details = {
  name: "Srinath",
};   // TypeError: Assignment to constant variable.

// correct syntax
details.age = 21;
console.log(details);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
