<?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: himanshuc11</title>
    <description>The latest articles on DEV Community by himanshuc11 (@himanshuc11).</description>
    <link>https://dev.to/himanshuc11</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%2F806812%2Ffc47183c-cf32-4e84-807b-0e08d136a8b1.png</url>
      <title>DEV Community: himanshuc11</title>
      <link>https://dev.to/himanshuc11</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/himanshuc11"/>
    <language>en</language>
    <item>
      <title>Hoisting and Temporal Dead Zone</title>
      <dc:creator>himanshuc11</dc:creator>
      <pubDate>Wed, 02 Feb 2022 06:27:23 +0000</pubDate>
      <link>https://dev.to/himanshuc11/hoisting-and-temporal-dead-zone-4dcn</link>
      <guid>https://dev.to/himanshuc11/hoisting-and-temporal-dead-zone-4dcn</guid>
      <description>&lt;p&gt;Hoisting is effectively the result of JavaScript Engine's 2 phase Execution of the program(&lt;a href="https://dev.to/himanshuc11/working-of-js-engine-258h"&gt;https://dev.to/himanshuc11/working-of-js-engine-258h&lt;/a&gt;).&lt;/p&gt;

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

&lt;p&gt;When variables are encountered during the program run, during the first pass they are given space in memory and initialized with undefined&lt;/p&gt;

&lt;p&gt;The variables are updated with their actual values only when the thread of execution reaches the line of code, where they are initialized.&lt;/p&gt;

&lt;p&gt;So effectively during hoisting we feel as if the variables are "hoisted" to the top, but actually during the first pass, all variables are assigned undefined and stored in memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is hoisted?
&lt;/h3&gt;

&lt;p&gt;Although all three types of declaration, let, const and var are hoisted, var behaves differently from let and const. var declarations are initialized with undefined, but let and const are not initialized with a default value, instead an exception will be thrown when we try to access let and const before its initialization from the thread of execution inside from the source file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// The first pass occurs and thread of execution starts from here
// Here vx stores undefined
var dev = "dev"    // vx now stores "dev"
&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;// The first pass is over
// Here the Engine knows about cx and lx, but cannot assign a value to them
const cx = 'dev' // now cx has its value as 'dev'
const lx = 'dev' // now lx has its value as 'dev'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Temporal Dead Zone
&lt;/h3&gt;

&lt;p&gt;When trying to access let and const variables, before its initialization from the source file's thread of execution, will result in a reference error. The variable is in the Temporal Dead Zone from the start of its initialization in the first pass, till the thread of execution assigns some value to it. Here JavaScript knows that a variable exists, but doesn't know what it data/value it holds&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Line 1, here Engine knows about x, but not its value, start of temporal dead zone
.
.
.
let x = 'dev'
// After assigning x some value, temporal dead zone is over, as its declaration is complete
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Common Confusion
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;undefined refers to something that doesn't exist yet, or doesn't exist anymore&lt;/li&gt;
&lt;li&gt;null refers to an empty value&lt;/li&gt;
&lt;li&gt;not defined refers to an exception in which JavaScript Engine doesn't know about the identifier requested&lt;/li&gt;
&lt;li&gt;reference error refers to an exception in which, let or const is accessed before its declaration is complete&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To get a video tutorial on the concept&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=wtBbanu-kUY"&gt;https://www.youtube.com/watch?v=wtBbanu-kUY&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;References&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting"&gt;https://developer.mozilla.org/en-US/docs/Glossary/Hoisting&lt;/a&gt; &lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Working of JS Engine</title>
      <dc:creator>himanshuc11</dc:creator>
      <pubDate>Tue, 01 Feb 2022 13:07:33 +0000</pubDate>
      <link>https://dev.to/himanshuc11/working-of-js-engine-258h</link>
      <guid>https://dev.to/himanshuc11/working-of-js-engine-258h</guid>
      <description>&lt;p&gt;JavaScript runs through the code it needs to execute, twice. This results in two phase execution of code&lt;/p&gt;

&lt;p&gt;The two phases are popularly referred as &lt;br&gt;
1) Creation Phase&lt;br&gt;
2) Execution Phase&lt;/p&gt;

&lt;h3&gt;
  
  
  Creation Phase
&lt;/h3&gt;

&lt;p&gt;In the creation phase, the JavaScript Engine goes through the code to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a special object called activation object. This object is used to hold all the variables and functions&lt;/li&gt;
&lt;li&gt;Create a scope chain, which is an array of lexical environments.(Will be discussed in detail in later blogs)&lt;/li&gt;
&lt;li&gt;Determine the value of 'this', which is a special keyword in JavaScript which refers to the object calling the function&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Execution Phase
&lt;/h3&gt;

&lt;p&gt;In this phase, the JavaScript Engine goes through the code line by line(in the order dictated by the thread of execution) and executes each line of code, and updates the values of variables as needed&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages of 2 Phase Execution Process
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;As we can see, during the creation phase, JavaScript has access to all the variables and functions, we can get access to those functions. Hence we can now call a function before its definition.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Function call before, definition is valid
dev()
function dev() {
// Some Code
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hence call before definition is now possible in JavaScript Engine&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;JavaScript now also knows all your variables and sets a initial value 'undefined' to it. This way if some identifier is not declared, the error message would give 'not defined' instead of 'undefined'
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(x)  // This gives undefined
let x = "dev"
console.log(x) // This gives "dev", as it is updated on the line above
&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(y) // This is not defined as 'y' is not a valid identifier that JavaScript knows about
let x = "dev"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get a video tutorial on the concept&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=wtBbanu-kUY"&gt;https://www.youtube.com/watch?v=wtBbanu-kUY&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://medium.com/@happymishra66/execution-context-in-javascript-319dd72e8e2c"&gt;https://medium.com/@happymishra66/execution-context-in-javascript-319dd72e8e2c&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/@sudhakarsp06/creation-phase-and-execution-phase-in-javascript-32fcdbef60f4"&gt;https://medium.com/@sudhakarsp06/creation-phase-and-execution-phase-in-javascript-32fcdbef60f4&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Variable Declarations and Scoping in JavaScript</title>
      <dc:creator>himanshuc11</dc:creator>
      <pubDate>Mon, 31 Jan 2022 13:07:24 +0000</pubDate>
      <link>https://dev.to/himanshuc11/variable-declarations-and-scoping-in-javascript-dmh</link>
      <guid>https://dev.to/himanshuc11/variable-declarations-and-scoping-in-javascript-dmh</guid>
      <description>&lt;p&gt;There are only three types of variable declaration inside JavaScript. These are &lt;br&gt;
let, const and var declarations&lt;/p&gt;

&lt;p&gt;Before we understand these declarations we need to know about scoping.Scoping simply answers the basic question, "Where is this variable name valid?". This can also be thought of where can I, access this variable from.&lt;/p&gt;

&lt;p&gt;There are 2 types of scoping &lt;/p&gt;
&lt;h3&gt;
  
  
  Block Scoping
&lt;/h3&gt;

&lt;p&gt;In this type of scoping, the variables are valid only to the nearest enclosing block and not outside it. They are essentially valid only from "{" to the "}". So we simply look for the innermost "{" in which the identifier is present. It is now valid until it's corresponding "}" is not encountered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
// Block Scope of a variable 
}

1{
   // Variables declared here are valid from 1  to 1' and even valid inside 2 to 2' as its enclosing block is 1'
    2{
        // Variables declared here valid only from 2 to 2'
    2'}
1'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lexical Scoping
&lt;/h3&gt;

&lt;p&gt;Lexical scoping allows variables to be valid inside the whole function inside which they are declared and ignore all block level scoping. Here the variable once declared inside the function can be accessed inside any level of the function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function dev() 1{
// Lexically scoped variables, are valid from 1-1' 
1'}

function dev1() 1{
    2{
        // Lexically declared variable x
    2'}
    // x is valid here as it is lexically valid from 1-1' 
1'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we understand scoping so we can discuss let, const and var in detail.&lt;/p&gt;

&lt;p&gt;let and const are block level scoped and var is lexically scoped.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difference between let and const
&lt;/h3&gt;

&lt;p&gt;let is used to refer to variables that may change in the future.&lt;/p&gt;

&lt;p&gt;const is used to declare a read-only reference to the value. A const statement does not allow re declaration or re-assignment of variables&lt;/p&gt;

&lt;h3&gt;
  
  
  Common confusion
&lt;/h3&gt;

&lt;p&gt;Using const in objects makes a constant reference to the object, not the object constant. The object is still mutable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This is valid JS as we are mutating the object, not the reference
const obj = {}
obj.key = "value"

// This is invalid JS, as we are mutating the reference itself
const obj1 = {}
obj1 = {"key": "value"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another common confusion is that var creates global variables, which is not entirely true.&lt;br&gt;
var is used to create variables with lexical/functional scope, so var becomes a global variable only when it is declared outside any function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// variables declared here are globally scoped

function dev() {
// variables declared here are not globally scoped, and are restricted to the dev
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To get a video tutorial on this concept,&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=wtBbanu-kUY"&gt;https://www.youtube.com/watch?v=wtBbanu-kUY&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Scope"&gt;https://developer.mozilla.org/en-US/docs/Glossary/Scope&lt;/a&gt; &lt;br&gt;
&lt;a href="https://javascript.info/closure"&gt;https://javascript.info/closure&lt;/a&gt;&lt;/p&gt;

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